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; } }