在SpringBoot中简单使用Spring Security(随笔)

本文介绍了如何在SpringBoot项目中集成SpringSecurity,通过添加相关依赖并配置SecurityConfig类,实现了不同级别的资源访问权限控制。配置包括:首页开放访问,其他页面根据角色权限访问,自定义登录页面,禁用CSRF,启用记住我功能,以及注销功能。同时,通过内存中的用户认证信息设置了不同用户的角色和权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在SpringBoot中简单使用Spring Security(随笔)

  • 导入相关依赖 ;
    <dependency>
         	<groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  • 定义一个Spring Security配置类继承自WebSecurityConfigurerAdapter类,在此类上加@EnableWebSecurity注解;并在此类中进行一些简单的配置。
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    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.crypto.bcrypt.BCryptPasswordEncoder;
    
    @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");
            //formLogin()当用户没有权限进入相应页面时他会跳转到spring security自己的登陆页面;
            //loginPage("跳转到登陆页面的请求")会重定向到用户相应的登录页面,;
            //需要提交登录请求到loginProcessingUrl("登录请求"),请求方式为必须Post,
            http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");
            //防止跨站请求伪造攻击
            http.csrf().disable();
            //将登录用户信息保存到Cookie
            http.rememberMe();
            //Spring Security注销方法logout()logoutSuccessUrl("指定路径")注销成功后到指定页面
            http.logout().logoutSuccessUrl("/");
        }
        //认证
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            //在内存中获取用户信息,对对应的用户可以访问的权限进行授权,
            //指定相应的用户的密码时需被加密,
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("xiaoguo")
                	.password(new BCryptPasswordEncoder()
                    		.encode("123456")).roles("vip1","vip2")
                    .and()
                    .withUser("root")
                	.password(new BCryptPasswordEncoder()
                      		.encode("123456")).roles("vip1","vip2","vip3")
                    .and()
                    .withUser("guest")
                	.password(new BCryptPasswordEncoder()
                            .encode("123456")).roles("vip1");
        }
    }
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值