|
@@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import edu.travel.EncryptUtil;
|
|
|
import edu.travel.tenant.entity.EduTenant;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
@@ -21,6 +23,8 @@ import java.io.IOException;
|
|
|
|
|
|
@Component
|
|
|
public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
@Override
|
|
|
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
|
|
|
String token = httpServletRequest.getHeader("token");
|
|
@@ -30,13 +34,23 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
JSONObject jsonObject = JSON.parseObject(json);
|
|
|
//用户身份信息
|
|
|
String username = jsonObject.getString("principal");
|
|
|
- EduTenant tenant = new EduTenant();
|
|
|
- tenant.setTenantPhone(username);
|
|
|
+ Object object = redisTemplate.opsForValue().get(username + "_info");
|
|
|
+ if (object == null){
|
|
|
+ JSONObject resultObject = new JSONObject();
|
|
|
+ resultObject.put("code",401);
|
|
|
+ resultObject.put("msg","not found user");
|
|
|
+ resultObject.put("data",null);
|
|
|
+ httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ httpServletResponse.setContentType("application/json;charset=utf-8");
|
|
|
+ httpServletResponse.getWriter().write(resultObject.toJSONString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ EduTenant eduTenant = JSON.parseObject(object.toString(), EduTenant.class);
|
|
|
//用户权限
|
|
|
JSONArray authoritiesArray = jsonObject.getJSONArray("authorities");
|
|
|
String[] authorities = authoritiesArray.toArray(new String[authoritiesArray.size()]);
|
|
|
//将用户信息和权限填充 到用户身份token对象中
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(tenant, null, AuthorityUtils.createAuthorityList(authorities));
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(eduTenant, null, AuthorityUtils.createAuthorityList(authorities));
|
|
|
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
|
|
|
//将authenticationToken填充到安全上下文
|
|
|
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|