• springboot使用springsecuity


    1.使用maven配置SpringSecurity依赖,在pom.xml文件中添加:

    <!--SpringSecurity-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    2.编写controller,添加配置类:

     
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.core.userdetails.User;
    
    
    @Configuration
    @EnableWebSecurity
    public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    
        /**
         * 先通过内存中的账号密码来处理
         * @param auth
         * @throws Exception
         */
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("zhang")
                    .password("{noop}123")
                    .roles("USER");
        }
    
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/login.jsp","/failuer.jsp","/css/**","/img/**")
                    .permitAll()
                    .antMatchers("/**").hasAnyRole("USER")
                    .anyRequest()
                    .authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login.jsp")
                    .loginProcessingUrl("/login")
                    .successForwardUrl("/home.jsp")
                    .failureForwardUrl("/failure.jsp")
                    .permitAll()
                    .and()
                    .logout()
                    .logoutUrl("/logout")
                    .invalidateHttpSession(true)
                    .logoutSuccessUrl("/login.jsp")
                    .permitAll()
                    .and()
                    .csrf()
                    .disable();
    
    
        }
    }
  • 相关阅读:
    将数据加载到内存中
    反射,Java开发者必须跨越的坎
    Eclipse中Java文件图标由实心J变成空心J的问题
    Maven项目 解决cannot be read or is not a valid ZIP file问题
    MyEclipse中SVN
    常用的 正则验证等
    前台传递数组后台通过json字符串承接和处理
    img标签图片与图片背景的区别和使用场景
    数据库的特性和事务的特性
    redis在EOS7上面进行开启和停止的命令
  • 原文地址:https://www.cnblogs.com/zhukaile/p/16279648.html
Copyright © 2020-2023  润新知