spring-security

依赖

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

springsecurity配置

@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.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");
         //防止跨域请求攻击
         http.csrf().disable();
         //开启注销功能
         http.logout().logoutSuccessUrl("/");
         //开启记录我功能
         http.rememberMe().rememberMeParameter("rembmer");
    }

    //认证
    //密码编码: PasswordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在内存中验证
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("wenghuangge").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1");
    }
}

 

posted @ 2020-06-27 16:37  Dorom  阅读(116)  评论(0)    收藏  举报