Blame view

src/main/java/com/objecteye/handle/LoginSuccessHandler.java 1.63 KB
00762fb4   Liu Haoyu   代码重构;
1
  package com.objecteye.handle;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
2
3
  
  import com.alibaba.fastjson.JSON;
00762fb4   Liu Haoyu   代码重构;
4
5
6
  import com.alibaba.fastjson.JSONObject;
  import com.objecteye.utils.GlobalUtil;
  import org.springframework.data.redis.core.RedisTemplate;
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  import org.springframework.security.core.Authentication;
  import org.springframework.security.jwt.JwtHelper;
  import org.springframework.security.jwt.crypto.sign.RsaSigner;
  import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  /**
   * 登录成功处理
   */
  public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
  
      private RsaSigner signer;
  
00762fb4   Liu Haoyu   代码重构;
23
24
      private RedisTemplate redisTemplate;
  
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
25
26
27
28
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
          response.setContentType("application/json;charset=UTF-8");
          String userJsonStr = JSON.toJSONString(authentication.getPrincipal());
00762fb4   Liu Haoyu   代码重构;
29
30
31
          JSONObject userJson = JSON.parseObject(userJsonStr);
          // 保存登录存根
          redisTemplate.opsForHash().put(GlobalUtil.LOGIN_USER_TOKEN, userJson.getString("username"), userJson.getLongValue("createTime"));
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
32
33
34
35
36
37
38
39
          String token = JwtHelper.encode(userJsonStr, signer).getEncoded();
          //签发token
          response.getWriter().write("token=" + token);
      }
  
      public void setSigner(RsaSigner signer) {
          this.signer = signer;
      }
00762fb4   Liu Haoyu   代码重构;
40
41
42
43
  
      public void setRedisTemplate(RedisTemplate redisTemplate) {
          this.redisTemplate = redisTemplate;
      }
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
44
  }