c83b5b39
Liu Haoyu
项目创建, 集成spring-se...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.objecteye.config;
import com.alibaba.fastjson.JSON;
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;
@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());
String token = JwtHelper.encode(userJsonStr, signer).getEncoded();
//签发token
response.getWriter().write("token=" + token);
}
public void setSigner(RsaSigner signer) {
this.signer = signer;
}
}
|