UsernamePasswordAuthenticationToken
类来封装用户名和密码的认证信息发起登入请求后,进入到login()方法
/**
* 在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,
* 所以需要在SecurityConfig中配置把AuthenticationManager注入容器。
* @param user
* @return
*/
@Override
public ResponseResult login(User user) {
//通过AuthenticationManager的authenticate方法来进行用户认证
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
Authentication authenticate = authenticationManager.authenticate(token);
//如果没通过,给出对应的提示
if(Objects.isNull(authenticate)){
throw new RuntimeException("用户名或密码错误");
}
//如果通过了,使用userId生成一个jwt,存入ResponseResult返回
LoginUser loginUser = (LoginUser)authenticate.getPrincipal();
String id = loginUser.getUser().getId().toString();
//根据用户id生成token
String jwt = JwtUtil.createJWT(id);
//把完整的用户信息存入redis,使用userId作为key
redisCache.setCacheObject("login:"+id,loginUser);
ResponseResult responseResult = new ResponseResult();
responseResult.setCode(200);
responseResult.setMsg("登入成功");
Map<String,String> map = new HashMap<>();
map.put("token",jwt);
responseResult.setData(map);
return responseResult;
}
在执行 Authentication authenticate = authenticationManager.authenticate(token);时,会调用security框架的loadUserByUsername
方法查询用户信息,这里我们重写loadUserByUsername
方法
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Autowired
private MenuMapper menuMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//查询用户信息
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getUserName,username);
User user = userMapper.selectOne(lambdaQueryWrapper);
//如果没有查询到用户就抛出异常
if (Objects.isNull(user)){
throw new RuntimeException("用户名或密码错误");
}
//查询对应的权限信息
List<String> list = menuMapper.selectPermsByUserId(user.getId());
//把数据封装成UserDetails返回
return new LoginUser(user,list);
}
}
这样执行完loadUserByUsername
方法之后,就把权限信息封装到LoginUser中
调用接口之后返回一个token信息
{
"code": 200,
"msg": "登入成功",
"data": {
"token": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiODhkZmIxZjExMDg0NmZhOTJlN2I5OTM4M2Q3ZTQ5NyIsInN1YiI6IjQiLCJpc3MiOiJ3aHMiLCJpYXQiOjE3MTg2MjQyMTAsImV4cCI6MTcxODYyNzgxMH0.Z-qa-1rD6dIOxKSaeZ_wjHFKs_TY31hFgZnl2Yld4M4"
}
}
看以下UsernamePasswordAuthenticationToken
源码的实现思想
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
}
public abstract class AbstractAuthenticationToken implements Authentication, CredentialsContainer {
}
这段代码同时可以参考ArrayList的实现思想
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
}
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
}
abstract class Animal {
String name;
void sleep() {
System.out.println(name + " is sleeping.");
}
abstract void makeSound();
}
interface Pet {
void play();
}
public class Dog extends Animal implements Pet {
public Dog(String name) {
this.name = name;
}
@Override
void makeSound() {
System.out.println(name + " says: Woof!");
}
@Override
public void play() {
System.out.println(name + " is playing.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.sleep(); // Animal类的方法
dog.makeSound(); // Dog类实现的抽象方法
dog.play(); // Pet接口的方法
Pet pet = dog; // 可以用接口类型引用对象
pet.play(); // 接口的方法
}
}
前端登入获取token之后,之后其他请求在访问接口时候都需要带上token
在SecurityConfig类上加上如下注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
/**
* 测试
* @return
*/
@RequestMapping("/hello")
@PreAuthorize("ex.hasAuthority('sysytem:dept:list')")
public String hello(){
return "hello";
}
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Autowired
private RedisCache redisCache;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//获取token
String token = request.getHeader("token");
if(!StringUtils.hasText(token)){
//放行
filterChain.doFilter(request,response);
return;
}
//解析token
String userId;
try {
Claims claims = JwtUtil.parseJWT(token);
userId= claims.getSubject();
} catch (Exception e) {
throw new RuntimeException("token非法");
}
//从redis中获取用户信息
String redisKey = "login:"+userId;
LoginUser loginUser = redisCache.getCacheObject(redisKey);
if(Objects.isNull(loginUser)){
throw new RuntimeException("用户未登入");
}
//存入SecurityContextHolder
//获取权限信息,封装到Authentication中
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser,null,loginUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//放行
filterChain.doFilter(request,response);
}
}
在header加上token之后可以请求到正确结果
如果不加token,则会报错401
springSecurity
获取项目完整代码