|
|
@@ -1,6 +1,7 @@
|
|
|
package seecoder.devcloud.web.infrastructure.security;
|
|
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
+import net.minidev.json.JSONObject;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
@@ -14,12 +15,14 @@ import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
import seecoder.devcloud.common.util.JwtTokenUtil;
|
|
|
import seecoder.devcloud.web.model.enums.UserIdentity;
|
|
|
import seecoder.devcloud.web.model.po.user.User;
|
|
|
+import seecoder.devcloud.web.model.vo.Response;
|
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -47,26 +50,43 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
|
|
if (StringUtils.isNotEmpty(authHeader)) {
|
|
|
// 有token,验证正确性,签名不正确,会抛异常
|
|
|
Claims claims = jwtTokenUtil.getClaimsFromToken(authHeader);
|
|
|
+ if (claims != null){
|
|
|
+ UserDetails userDetails = User.builder()
|
|
|
+ .id(claims.get("id",Integer.class))
|
|
|
+ .username(claims.get("name",String.class))
|
|
|
+ .email(claims.get("email",String.class))
|
|
|
+ .phone(claims.get("phone",String.class))
|
|
|
+ //todo 验证是否和门户身份一致
|
|
|
+ .role(UserIdentity.valueOf(claims.get("role",String.class)))
|
|
|
+ .build();
|
|
|
+ // 验证token是否过期
|
|
|
+ if (jwtTokenUtil.isTokenExpired(authHeader)) {
|
|
|
+ //加载用户、角色、权限信息,Spring Security根据这些信息判断接口的访问权限
|
|
|
+ UsernamePasswordAuthenticationToken authentication
|
|
|
+ = new UsernamePasswordAuthenticationToken(userDetails, null,
|
|
|
+ userDetails.getAuthorities());
|
|
|
+ authentication.setDetails(new WebAuthenticationDetailsSource()
|
|
|
+ .buildDetails(httpServletRequest));
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
+ } else {
|
|
|
+ //todo test token
|
|
|
+ PrintWriter out = null ;
|
|
|
+ try{
|
|
|
+ httpServletResponse.setCharacterEncoding("UTF-8");
|
|
|
+ httpServletResponse.setContentType("application/json; charset=utf-8");
|
|
|
+ out = httpServletResponse.getWriter();
|
|
|
+ out.append(Response.buildFailure(10110,"Jwt token 过期").toString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ httpServletResponse.sendError(500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- UserDetails userDetails = User.builder()
|
|
|
- .id(claims.get("id",Integer.class))
|
|
|
- .username(claims.get("name",String.class))
|
|
|
- .email(claims.get("email",String.class))
|
|
|
- .phone(claims.get("phone",String.class))
|
|
|
- //todo 验证是否和门户身份一致
|
|
|
- .role(UserIdentity.valueOf(claims.get("role",String.class)))
|
|
|
- .build();
|
|
|
|
|
|
- // 验证token是否过期
|
|
|
- if (jwtTokenUtil.isTokenExpired(authHeader)) {
|
|
|
- //加载用户、角色、权限信息,Spring Security根据这些信息判断接口的访问权限
|
|
|
- UsernamePasswordAuthenticationToken authentication
|
|
|
- = new UsernamePasswordAuthenticationToken(userDetails, null,
|
|
|
- userDetails.getAuthorities());
|
|
|
- authentication.setDetails(new WebAuthenticationDetailsSource()
|
|
|
- .buildDetails(httpServletRequest));
|
|
|
- SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
- }
|
|
|
|
|
|
}
|
|
|
filterChain.doFilter(httpServletRequest, httpServletResponse);
|