上一篇:SpringSecurity集成JWT实现后端认证授权保姆级教程-认证配置篇
🤞上一节编写了对应的SpringSecurity的认证,本节开始进行SpringSecurity授权
🤞
修改对应的getAuthorities方法
@TableName(value ="cust_user")
@Data
public class CustUser implements Serializable, UserDetails { //这里新增实现UserDetails
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
private String nickname;
private Integer enable;
private String password;
//----------------------------------------------------以下为新增的部分---------------------------------
@TableField(exist = false)
private List<String> permissions; //权限集合
//存储SpringSecurity所需要的权限信息的集合
//安全限制,不允许序列化
@JSONField(serialize = false)
@JsonIgnore
@TableField(exist = false)
private List<GrantedAuthority> authorities;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
if (this.authorities == null) {
//把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
this.authorities = this.permissions.stream().distinct().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
}
return this.authorities;
}
//------------------------------------------------------------------------------------------------
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
对CustUserServiceImpl 中的loadUserByUsername进行修改
针对之前的数据准备篇的CustUserServiceImpl实现类 进行更改
@Service
public class CustUserServiceImpl extends ServiceImpl<CustUserMapper, CustUser> implements CustUserService{ //这个地方新增实现CustUserService接口
@Autowired
private CustUserMapper custUserMapper;
@Autowired
private SysMenuMapper menuMapper;
//-----------------------------------------------------以下为修改的内容------------------------------------------
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
LambdaQueryWrapper<CustUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CustUser::getUsername, username);
CustUser user = custUserMapper.selectOne(queryWrapper);
if (user == null) {
log.error("用户名不存在");
throw new UsernameNotFoundException("用户名不存在");
}else {
List<String> permissions = menuMapper.selectPermsByUserId(user.getId());
user.setPermissions(permissions); //封装权限
return user;
}
}
//----------------------------------------------------------------------------
}
上边的这个主要是实现loadUserByUsername方法中获取数据库中用户的权限。 添加之后会发现上边的menuMapper.selectPermsByUserId(user.getId());报错,因为我们还没有在menuMapper新增selectPermsByUserId方法,接下来我们新增一个查询用户权限的方法。
在mapper/SysMenuMapper中的新增方法selectPermsByUserId
public interface SysMenuMapper extends BaseMapper<SysMenu> {
List<String> selectPermsByUserId(Integer id);
}
在resources/SysMenuMapper/xml中的新增方法selectPermsByUserId
<select id="selectPermsByUserId" resultType="java.lang.String">
SELECT
DISTINCT m.`perms`
FROM
sys_user_role ur
LEFT JOIN `sys_role` r ON ur.`role_id` = r.`id`
LEFT JOIN `sys_role_menu` rm ON ur.`role_id` = rm.`role_id`
LEFT JOIN `sys_menu` m ON m.`id` = rm.`menu_id`
WHERE
user_id = #{id}
AND r.`status` = 0
AND m.`status` = 0
</select>
在数据库中的增加test权限
@Api(tags = "测试类")
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private CustUserService custUserService;
@ApiOperation(value = "测试方法")
@PreAuthorize("hasAuthority('test')") // 权限验证 需要用户具有test权限
@GetMapping("/test")
public CustUser test(){
return custUserService.getById(1);
}
}
到这授权就已经好了。如果还想根据角色进行权限认证,可自己百度,后边有时间我会更新。
大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。
Writted By 知识浅谈