壹.Spring-Security权限管理功能
一.简介
1.有些网页必须登录之后才能访问
即使都是登录用户,它们也有可能有身份的区别,不同身份有不同功能
2.只有学生才能访问发布问题的页面,
只有老师才能访问回答问题的页面
3.Spring-Security也方便我们来管理这些问题
二.访问用户信息
下面我们就使用Spring-Security权限管理功能访问用户信息
1.开启权限管理功能
1.SecurityConfig这类中编写代码
//@Configuration表示当前类是配置类,可能向Spring容器中注入对象
@Configuration
//下面的注解表示通知Spring-Security开启权限管理功能
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends
WebSecurityConfigurerAdapter {//网页安全配置适配器
@Override//重写方法
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
//方法中配置哪个用户可以有什么样的权限
auth.inMemoryAuthentication().withUser("Tom")
.password("{bcrypt}$2a$10$IHMiKBqpiPFYgRg4P0E0HeU.xdkr1nw0/y1AWKIvHh5TMNwxVuBRW")
.authorities("/tag/get");
}
}
2.TagController编写权限
1.在Controller类的方法上声明访问这个方法需要什么权限
2.如果访问这个方法需要"/tag/get"权限,那么没有这个权限的人就不能访问
3.如果Controller类的方法上没有声明权限,那么就是登录的用户都可以访问
4.在TagController类中编写一些权限
代码如下
//@RestController注解相当于@ResponseBody + @Controller合在一起的作用
@RestController
//下面的注解表示想访问本控制器中的任何方法需要前缀/portal/tag
@RequestMapping("/portal/tag")
public class TagController {
@Autowired
private IUserService userService;
@Autowired(required = false)
private ITagService tagService;
/**
* 权限:/tag/get
*/
@GetMapping("/get")//localhost/portal/tag/get?id=3
@PreAuthorize("hasAuthority('/tag/get')")//设置了权限
public User get(Integer id){
return userService.getById(3);
}
/**
* 权限:/tag/byid
*/
// 编写一套可以按tagid查询一个tag信息的控制器方法
// 将tag信息显示在浏览器
@GetMapping("/gettag")
@PreAuthorize("hasAuthority('/tag/byid')")//设置了权限
public Tag getTag(Integer id){
return tagService.getById(id);
}
}
3.测试运行
1.tom正常访问/get?id=3
2.访问/gettag?id=14时,发生403错误,表示权限不足
3.现在Tom用户不能访问的localhost/portal/tag/gettag路径,因为他没有权限
4.我们可以通过配置将这个权限赋予Tom用户
方法如下
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
//方法中配置哪个用户可以有什么样的权限
auth.inMemoryAuthentication().withUser("Tom")
.password("{bcrypt}$2a$10$IHMiKBqpiPFYgRg4P0E0HeU.xdkr1nw0/y1AWKIvHh5TMNwxVuBRW")
.authorities("/tag/get","/tag/byid");
}
三.使用UserDetailsService提供认证数据
1.编写类UserDetailsServiceImpl
1.我们在service包中编写一个类UserDetailsServiceImpl
代码如下
/**
* 这个类实现UserDetailsService接口
* 为Spring-Security提供认证的数据
*/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
//Spring-Security认证信息时
//会将用户名传递到这个方法中
//根据这个用户名获得数据库中加密的密码,
//如果匹配则登录成功
@Override
public UserDetails loadUserByUsername(String username)//默认变量s改为username
throws UsernameNotFoundException {
//这个方法返回的是用户的详细信息
UserDetails user=null;
//暂时规定正确的用户名和密码是"Jerry",加密的"123456"
if("Jerry".equals(username)){
//如果用户名正确,将加密的密码保存到User对象
//下面的User类也是Spring提供的不是我们的实体类
user= User.builder()
.username("Jerry")
.password("{bcrypt}$2a$10$IHMiKBqpiPFYgRg4P0E0HeU.xdkr1nw0/y1AWKIvHh5TMNwxVuBRW")
//这里也能直接赋予权限
.authorities("/tag/get")
.build();
}
System.out.println("user:"+user);
return user;
}
}
2.删除configer方法之前的代码
1.回到SecurityConfig类中,删除configer方法之前的代码
/*@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
//方法中配置哪个用户可以有什么样的权限
auth.inMemoryAuthentication().withUser("Tom")
.password("{bcrypt}$2a$10$0WTDZPEUWKW.K5jZ.P4TOuoYqAIKv4jmqFgQZnf9qWCvUHhE9l.8a")
.authorities("/tag/get","/tag/byid");
}
*/
2.将方法改写为:
//@Configuration表示当前类是配置类,可能向Spring容器中注入对象
@Configuration
//下面的注解表示通知Spring-Security开启权限管理功能
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends
WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService);
}
}