1 Star 0 Fork 0

simen/springboot-sample

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
JwtUserDetailsService.java 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
package com.strong.config.security.userdetails;
import cn.hutool.core.util.StrUtil;
import com.strong.utils.security.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 用户验证服务
*
* @author simen
* @date 2022/01/29
*/
@Slf4j
@Service
@Component("JwtUserDetailsService")
public class JwtUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 用户名非空时,强制注册simen和admin用户
if (StrUtil.isNotBlank(username)) {
// 用户权限队列
List<GrantedAuthority> listGrantedAuthority = new ArrayList<>();
Map<String, Object> mapProperties = new HashMap<>(8);
if (StrUtil.equals(username, "simen")) {
listGrantedAuthority.add(new SimpleGrantedAuthority("file_read"));
mapProperties.put("扩展属性", username + " file_read");
log.info("读取到已有用户[{}],默认密码123456,file_read权限,扩展属性:[{}]", username, mapProperties);
return new JwtUserDetails(username, SecurityUtils.signByUUID("123456"), false, listGrantedAuthority, mapProperties);
} else if (StrUtil.equals(username, "admin")) {
listGrantedAuthority.add(new SimpleGrantedAuthority("file_write"));
mapProperties.put("扩展属性", username + " file_write");
log.info("读取到已有用户[{}],默认密码123456,file_write权限,扩展属性:[{}]", username, mapProperties);
return new JwtUserDetails(username, SecurityUtils.signByUUID("123456"), false, listGrantedAuthority, mapProperties);
} else {
log.info("用户[{}]不存在", username);
return new JwtUserDetails();
}
} else {
log.info("用户为空");
return new JwtUserDetails();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/simen_net/springboot-sample.git
git@gitee.com:simen_net/springboot-sample.git
simen_net
springboot-sample
springboot-sample
5.使用JWT进行授权认证

搜索帮助

344bd9b3 5694891 D2dac590 5694891