ludc
2023-09-12 7f9d6840cc01c25ac9816df60dbac3d040594fa3
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
package com.vci.ubcs.auth.endpoint;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
import java.util.Map;
 
*/
/**
 * 免密登录
 * @author ludc
 * @date 2023/9/12 18:03
 *//*
 
@Component
public class PwdFreeLoginEndpoint extends TokenEndpoint {
 
 
    @Autowired
    private AuthorizationServerTokenServices tokenServices;
 
    @Autowired
    private ClientDetailsService clientDetailsService;
 
    @Autowired
    private OAuth2RequestFactory requestFactory;
 
    @Autowired
    private TokenGranter tokenGranter;
 
    @RequestMapping(value = "/oauth/password-free-login",method = RequestMethod.GET)
    public ResponseEntity<OAuth2AccessToken> getPasswordFreeLogin(@RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        return postPasswordFreeLogin(parameters);
    }
 
    @RequestMapping(value = "/oauth/password-free-login",method = RequestMethod.POST)
    public ResponseEntity<OAuth2AccessToken> postPasswordFreeLogin(@RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        // 判断是否满足免密登录的条件
        if (isSkipLogin(parameters)) {
            // 生成访问令牌
            OAuth2AccessToken accessToken = createAccessToken(parameters);
            // 返回访问令牌
            return ResponseEntity.ok(accessToken);
        }
 
        // 不满足免密登录条件,返回错误信息
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }
 
    private boolean isSkipLogin(Map<String, String> parameters) {
        // 根据具体需求判断是否满足免密登录的条件
        // 例如,判断请求参数中是否包含某个特定的标识符
        return parameters.containsKey("skipLogin");
    }
 
    private OAuth2AccessToken createAccessToken(Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        // 构造请求参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            params.add(entry.getKey(), entry.getValue());
        }
 
        // 调用TokenEndpoint的postAccessToken方法生成访问令牌
        return postAccessToken(PrincipalUtils.getPrincipal(), params).getBody();
    }
 
    private ResponseEntity<OAuth2AccessToken> postAccessToken(Authentication authentication, MultiValueMap<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        // 构造请求
        TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, clientDetailsService.loadClientByClientId("your-client-id"));
 
        // 生成访问令牌
        OAuth2AccessToken accessToken = tokenGranter.grant("password", tokenRequest);
 
        // 返回访问令牌
        return ResponseEntity.ok(accessToken);
    }
 
}
*/