springSecurity

时间:2021-05-24 23:15:43   收藏:0   阅读:43

 

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

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");

//关闭防止网站攻击工具
http.csrf().disable();

/*//开启框架默认的登陆、登出页面
http.formLogin().defaultSuccessUrl("/main.html");*/
//定制自己的登陆页面,
/*loginPage()与loginProcessingUrl()的含义
1.以loginPage中自定义的页面,loginProcessingUrl()里面的数据,指登陆认证的页面。
* */
http.formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/main.html").loginProcessingUrl("/tologin");
http.logout().logoutSuccessUrl("/login.html");

//安全框架登陆页面的记住我功能,默认保存两周
http.rememberMe().rememberMeParameter("remember");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
.and()
.withUser("kuang").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip1");
}
}
 

 

  @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //关闭防止网站攻击工具
        http.csrf().disable();

        /*//开启框架默认的登陆、登出页面
        http.formLogin().defaultSuccessUrl("/main.html");*/
        //定制自己的登陆页面,
        /*loginPage()与loginProcessingUrl()的含义
            1.以loginPage中自定义的页面,loginProcessingUrl()里面的数据,指登陆认证的页面。
        * */
        http.formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/main.html").loginProcessingUrl("/tologin");
        http.logout().logoutSuccessUrl("/login.html");

        //安全框架登陆页面的记住我功能,默认保存两周
        http.rememberMe().rememberMeParameter("remember");
    }

 

  @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
                .and()
                .withUser("kuang").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip1");
    }

 

原文:https://www.cnblogs.com/youisme/p/14806197.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!