操作1:在pom中引入spring-security的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
什么都不做,现在启动系统会看到如下提示:
Using generated security password: e7da5da4-c93f-48a3-9616-e3d8f437a4df
随便找个controller写第一个hello world代码:
@GetMapping("/sayHello")
JsonResult sayHello(HttpServletRequest req)throws Exception{
return new JsonResult(ResultCode.SUCCESS,"hello world");
}
在浏览器访问:http://127.0.0.1/login
看到下边这个页面:

代表spring security已经生效
实验1:为你得security添加一个用户
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
PasswordEncoder PasswordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("123").roles("admin")
.and()
.withUser("zhangsan").password("123").roles("user");
}
}
继承了WebSecurityConfigurerAdapter这个类,重写其中的configure方法,这个方法有三个参数不同,我们先重写AuthenticationManagerBuilder 这个的
@Bean
PasswordEncoder PasswordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
PasswordEncoder代表了security密码加密策略,我们现在使用NoOpPasswordEncoder(不采用任何加密的方式)
现在进入之前的
http://127.0.0.1/login
这个页面,就不需要输入系统输出的默认密码了,输入我们刚才设置的admin密码123,就可以登录了
***权限测试01:***在上边代码中我们加入了admin用户.roles(“admin”) 这个东西,代表给当前用户赋值角色为admin角色
而zhangsan用户赋值权限为user
注意:需要在刚才SecurityConfig中添加注解:
@EnableGlobalMethodSecurity(prePostEnabled=true)
我们在控制器层加入注解:
@GetMapping("/sayHello")
@PreAuthorize("hasRole('ROLE_admin')")
JsonResult sayHello(HttpServletRequest req)throws Exception{
return new JsonResult(ResultCode.SUCCESS,"hello world");
}
@PreAuthorize(“hasRole(‘ROLE_admin’)”)
代表当前需要ROLE_admin角色才能访问(ROLE_是spring security默认自己加的,所有的角色都会默认添加这个前缀)
现在去访问,当张三登录的时候访问这个位置就会报错(因为我们没有做处理)

我们可以明确的看到其中错误code码为403,也就是访问权限被拒绝的意思
权限测试02
继续实现的configure方法,不同是这次里边的参数为:HttpSecurity http
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**").hasRole("admin")
.antMatchers("/sayHello").hasRole("admin")
.and().httpBasic()
.and().csrf().disable();
}
我们首先删除方法上的注解:@PreAuthorize("hasRole('ROLE_admin')")
这样配置为当前系统如果访问/admin/**这种路径需要admin角色,如果访问/sayHello路径也需要admin橘色
当我们使用zhangsan用户登录的时候,我们发现也是被拦截的
权限测试03
我们编写三个测试访问方法
@GetMapping("/sayHello")
JsonResult sayHello(Principal principal)throws Exception{
System.out.println(principal.getName());
return new JsonResult(ResultCode.SUCCESS,"hello world");
}
@GetMapping("/admin/say")
JsonResult sayAd(Principal principal)throws Exception{
System.out.println(principal.getName());
return new JsonResult(ResultCode.SUCCESS,"admin");
}
@GetMapping("/say")
JsonResult say(Principal principal)throws Exception{
System.out.println(principal.getName());
return new JsonResult(ResultCode.SUCCESS,"say");
}
第一个访问路径为/sayHello 第二个访问路径是以/admin开头,第三个没有什么特殊的,对应我们刚在configure(HttpSecurity http) 配置的类容:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**").hasRole("admin")
.antMatchers("/sayHello").hasRole("admin")
.and().httpBasic()
.and().csrf().disable();
}
这里我们添加用户的详情如下:
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("123").roles("admin")
.and()
.withUser("zhangsan").password("123").roles("user");
}
这时候测试,使用admin用户登录,三个请求位置都能访问到,但是使用zhangsan用户登录,仅仅只有/say这个路径能访问到
本文通过实战演示如何在Spring Boot项目中使用Spring Security进行权限管理,包括用户认证、角色分配及路径访问控制等内容。
5006

被折叠的 条评论
为什么被折叠?



