欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringSecurity

程序员文章站 2024-03-19 13:44:52
...
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // 认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       // 在内存中取值认证,正常情况下在数据库取值认证
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder(2))
                // 开启密码加密方法
                .withUser("root").password(new BCryptPasswordEncoder(2).encode("123456"))
                .roles("Admin");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // 请求授权规则
        http.authorizeRequests().antMatchers("/").permitAll()
                // 拥有指定角色才能访问
                .antMatchers("/level1").hasRole("user");
        // 无权限自动跳转登录界面
        http.formLogin();
        // 关闭csrf
        http.csrf().disable();
        // 开启记住我功能
        http.rememberMe();
        // logoutUrl("/"):注销界面,logoutSuccessUrl():注销成功后后跳转到的页面
        http.logout().logoutUrl("/").logoutSuccessUrl("/");
    }
}

相关标签: java