| | |
| | | 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(); |
| | | return null; |
| | | } |
| | | |
| | | 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); |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | //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(); |
| | | // return null; |
| | | // } |
| | | // |
| | | // 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); |
| | | // return null; |
| | | // } |
| | | // |
| | | //} |