zrj
2024-06-11 4069aa1d01bb4ce98ea154940a46d5c014252897
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.springblade.auth.config;
 
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
 
import static org.springframework.security.web.context.HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY;
 
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
 
    private String redirectUrl;
 
    private String springSecurityContextKey = SPRING_SECURITY_CONTEXT_KEY;
 
    public CustomAuthenticationSuccessHandler(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }
 
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        // 获取session
//        HttpSession session = request.getSession();
        // 从authentication中获取principal信息
//        Object principal = authentication.getPrincipal();
        // 将principal信息存入session
//        session.setAttribute(this.springSecurityContextKey, principal);
        //        String requestURI = request.getRequestURI();
        Map<String, String[]> parameterMap = request.getParameterMap();
        StringBuilder builder = new StringBuilder();
        if (parameterMap.size()>0){
            builder.append("?");
        }
        String client_id = request.getHeader("client_id");
        String response_type = request.getHeader("response_type");
        String redirect_uri = request.getHeader("redirect_uri");
        // 拼接
        if (!Strings.isBlank(client_id)){
            builder.append("client_id=").append(client_id)
                .append("&")
                .append("response_type=").append(response_type)
                .append("&")
                .append("redirect_uri=").append(redirect_uri);
        }
        // 跳转
        response.sendRedirect(redirectUrl + builder.toString());
    }
}