|
|
|
@ -1,12 +1,12 @@
|
|
|
|
|
package cn.hippo4j.auth.filter;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import cn.hippo4j.auth.model.biz.user.JwtUser;
|
|
|
|
|
import cn.hippo4j.auth.model.biz.user.LoginUser;
|
|
|
|
|
import cn.hippo4j.auth.toolkit.JwtTokenUtil;
|
|
|
|
|
import cn.hippo4j.auth.toolkit.ReturnT;
|
|
|
|
|
import cn.hippo4j.common.web.base.Results;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
@ -27,6 +27,7 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import static cn.hippo4j.auth.constant.Constants.SPLIT_COMMA;
|
|
|
|
|
import static cn.hippo4j.common.constant.Constants.BASE_PATH;
|
|
|
|
|
import static cn.hippo4j.common.constant.Constants.MAP_INITIAL_CAPACITY;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JWT authentication filter.
|
|
|
|
@ -37,9 +38,9 @@ import static cn.hippo4j.common.constant.Constants.BASE_PATH;
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
|
|
|
|
|
|
|
|
|
private AuthenticationManager authenticationManager;
|
|
|
|
|
private final AuthenticationManager authenticationManager;
|
|
|
|
|
|
|
|
|
|
private ThreadLocal<Integer> rememberMe = new ThreadLocal();
|
|
|
|
|
private final ThreadLocal<Integer> rememberMe = new ThreadLocal();
|
|
|
|
|
|
|
|
|
|
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
|
|
|
|
|
this.authenticationManager = authenticationManager;
|
|
|
|
@ -54,7 +55,7 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
|
|
|
|
LoginUser loginUser = new ObjectMapper().readValue(request.getInputStream(), LoginUser.class);
|
|
|
|
|
rememberMe.set(loginUser.getRememberMe());
|
|
|
|
|
return authenticationManager.authenticate(
|
|
|
|
|
new UsernamePasswordAuthenticationToken(loginUser.getUsername(), loginUser.getPassword(), new ArrayList<>())
|
|
|
|
|
new UsernamePasswordAuthenticationToken(loginUser.getUsername(), loginUser.getPassword(), new ArrayList())
|
|
|
|
|
);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("attemptAuthentication error :{}", e);
|
|
|
|
@ -67,22 +68,26 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
|
|
|
|
HttpServletResponse response,
|
|
|
|
|
FilterChain chain,
|
|
|
|
|
Authentication authResult) throws IOException {
|
|
|
|
|
JwtUser jwtUser = (JwtUser) authResult.getPrincipal();
|
|
|
|
|
boolean isRemember = rememberMe.get() == 1;
|
|
|
|
|
try {
|
|
|
|
|
JwtUser jwtUser = (JwtUser) authResult.getPrincipal();
|
|
|
|
|
boolean isRemember = rememberMe.get() == 1;
|
|
|
|
|
|
|
|
|
|
String role = "";
|
|
|
|
|
Collection<? extends GrantedAuthority> authorities = jwtUser.getAuthorities();
|
|
|
|
|
for (GrantedAuthority authority : authorities) {
|
|
|
|
|
role = authority.getAuthority();
|
|
|
|
|
}
|
|
|
|
|
String role = "";
|
|
|
|
|
Collection<? extends GrantedAuthority> authorities = jwtUser.getAuthorities();
|
|
|
|
|
for (GrantedAuthority authority : authorities) {
|
|
|
|
|
role = authority.getAuthority();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String token = JwtTokenUtil.createToken(jwtUser.getId(), jwtUser.getUsername(), role, isRemember);
|
|
|
|
|
response.setHeader("token", JwtTokenUtil.TOKEN_PREFIX + token);
|
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
Map<String, Object> maps = new HashMap<>();
|
|
|
|
|
maps.put("data", JwtTokenUtil.TOKEN_PREFIX + token);
|
|
|
|
|
maps.put("roles", role.split(SPLIT_COMMA));
|
|
|
|
|
response.getWriter().write(JSONUtil.toJsonStr(Results.success(maps)));
|
|
|
|
|
String token = JwtTokenUtil.createToken(jwtUser.getId(), jwtUser.getUsername(), role, isRemember);
|
|
|
|
|
response.setHeader("token", JwtTokenUtil.TOKEN_PREFIX + token);
|
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
Map<String, Object> maps = new HashMap(MAP_INITIAL_CAPACITY);
|
|
|
|
|
maps.put("data", JwtTokenUtil.TOKEN_PREFIX + token);
|
|
|
|
|
maps.put("roles", role.split(SPLIT_COMMA));
|
|
|
|
|
response.getWriter().write(JSONUtil.toJsonStr(Results.success(maps)));
|
|
|
|
|
} finally {
|
|
|
|
|
rememberMe.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|