背景: 项目中已经有了登录界面,通过账号密码登录的方式登录系统,之前别人搞的,
新需求: 需要与其他系统集成,相当于单点登录,也不需要去认证服务器验证,默认认为他们传过来的就是正确的(话说这样真的挺危险的), 经过讨论,加一点加密措施,使用了 JWT进行加密传输, 所以需要在保留现有登录界面登录方式的基础上, 添加新的登录方式,点击链接直接登录(链接中带上加密后的JWT串)
过程: 接到了需求后,就去网上搜了一遍, 毕竟面向百度编程时代, 看了看怎么集成, 帖子也不少,写的比较好的如下
https://blog.csdn.net/qq_36521507/article/details/103365805
https://blog.csdn.net/qq_36521507/article/details/103370070
照着上面的文章,抄了一下,写了三个类
JWTAuthenticationProcessingFilter
JWTAuthenticationProvider
JWTAuthenticationToken
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class JWTAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {
public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
private boolean postOnly = false;
public JWTAuthenticationProcessingFilter() {
super(new AntPathRequestMatcher("/jwtLogin", "GET"));
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
if (postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException(
"Authentication method not supported111: " + request.getMethod());
}
// String username = obtainUsername(request);
// Stri