【oauth2】oauth2修改获取token地址及返回信息

前言

转载:https://www.cnblogs.com/7788IT/articles/11626900.html

代码

在上面的代码上面,又加了一些常用的配置

  1. 资源服务配置
import com.oauth2.server.system.init.oauth2.handler.MyAuthenticationFailureHandler;
import com.oauth2.server.system.init.oauth2.handler.MyAuthenticationSucessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

/**
 * @author :qilong sun
 * @date :Created in 2020/7/10 16:56
 * @description:资源服务器配置;configure配置:https://www.cnblogs.com/7788IT/articles/11626900.html
 * @modified By:
 * @version: V1.0$
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Autowired
    private MyAuthenticationSucessHandler authenticationSucessHandler;
    @Autowired
    private MyAuthenticationFailureHandler authenticationFailureHandler;

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        // 开启授权认证
        httpSecurity.authorizeRequests().anyRequest().authenticated();
        // 登录地址
        httpSecurity.formLogin().loginProcessingUrl("/login");
        // 处理登录成功
        httpSecurity.formLogin().successHandler(authenticationSucessHandler);
        // 处理登录失败
        httpSecurity.formLogin().failureHandler(authenticationFailureHandler);
        // 开启跨域共享,跨域伪造请求限制=无效
        httpSecurity.cors().and().csrf().disable();
    }
}

  1. 成功处理
import com.alibaba.fastjson.JSONObject;
import com.basics.util.common.dto.RestCode;
import com.basics.util.common.dto.RestResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;

/**
 * @author :qilong sun
 * @date :Created in 2020/7/15 10:59
 * @description:
 * @modified By:
 * @version: V1.0$
 */
@Component
@Slf4j
public class MyAuthenticationSucessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    private AuthorizationServerTokenServices authorizationServerTokenServices;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        // 1. 从请求头中获取 ClientId
        String header = request.getHeader("Authorization");
        if (header == null || !header.startsWith("Basic ")) {
            throw new UnapprovedClientAuthenticationException("请求头中无client信息");
        }
        String[] tokens = this.extractAndDecodeHeader(header, request);
        String clientId = tokens[0];
        String clientSecret = tokens[1];
        TokenRequest tokenRequest = null;
        // 2. 通过 ClientDetailsService 获取 ClientDetails
        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
        // 3. 校验 ClientId和 ClientSecret的正确性
        if (clientDetails == null) {
            throw new UnapprovedClientAuthenticationException("clientId:" + clientId + "对应的信息不存在");
        } else if (!StringUtils.equals(clientDetails.getClientSecret(), clientSecret)) {
            throw new UnapprovedClientAuthenticationException("clientSecret不正确");
        } else {
            // 4. 通过 TokenRequest构造器生成 TokenRequest
            tokenRequest = new TokenRequest(new HashMap<>(), clientId, clientDetails.getScope(), "custom");
        }
        // 5. 通过 TokenRequest的 createOAuth2Request方法获取 OAuth2Request
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        // 6. 通过 Authentication和 OAuth2Request构造出 OAuth2Authentication
        OAuth2Authentication auth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
        // 7. 通过 AuthorizationServerTokenServices 生成 OAuth2AccessToken
        OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(auth2Authentication);
        // 8. 返回 Token
        log.info("登录成功");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        RestResponse restResponse = new RestResponse();
        restResponse.setCode(RestCode.SUCCESS.getCode());
        restResponse.setMsg("登录成功");
        restResponse.setData(new ObjectMapper().writeValueAsString(token));
        response.getWriter().write(JSONObject.toJSONString(restResponse));
    }

    private String[] extractAndDecodeHeader(String header, HttpServletRequest request) {
        byte[] base64Token = header.substring(6).getBytes(StandardCharsets.UTF_8);
        byte[] decoded;
        try {
            decoded = Base64.getDecoder().decode(base64Token);
        } catch (IllegalArgumentException var7) {
            throw new BadCredentialsException("Failed to decode basic authentication token");
        }
        String token = new String(decoded, StandardCharsets.UTF_8);
        int delim = token.indexOf(":");
        if (delim == -1) {
            throw new BadCredentialsException("Invalid basic authentication token");
        } else {
            return new String[]{token.substring(0, delim), token.substring(delim + 1)};
        }
    }
}

  1. 失败处理
import com.alibaba.fastjson.JSONObject;
import com.basics.util.common.dto.RestCode;
import com.basics.util.common.dto.RestResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author :qilong sun
 * @date :Created in 2020/7/15 10:59
 * @description:
 * @modified By:
 * @version: V1.0$
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Resource
    private ObjectMapper mapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        AuthenticationException e) throws IOException {
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
        RestResponse restResponse = new RestResponse();
        if(e instanceof AccessDeniedHandler){
            restResponse.setCode(RestCode.SUCCESS.getCode());
            restResponse.setMsg("登录成功");
        }else if(e instanceof AuthenticationEntryPoint){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("登录过期或未登录");
        }else if(e instanceof AccountExpiredException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("账户已过期");
        }else if(e instanceof BadCredentialsException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("用户名或密码错误");
        }else if(e instanceof DisabledException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("账户已禁用");
        }else if(e instanceof CredentialsExpiredException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("证书已过期");
        }else if(e instanceof LockedException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("用户账户已被锁定");
        }else if(e instanceof InternalAuthenticationServiceException){
            restResponse.setCode(RestCode.FAILURE.getCode());
            restResponse.setMsg("内部身份验证服务异常");
        }else{
            restResponse.setCode(RestCode.ERROR.getCode());
            restResponse.setMsg(RestCode.ERROR.getMsg());
        }
        httpServletResponse.getWriter().write(JSONObject.toJSONString(restResponse));
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Spring Security OAuth2来获取token。以下是基本的步骤: 1. 首先,您需要添加所需的依赖项。在您的项目的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 2. 在application.properties或application.yml文件中配置OAuth2客户端的信息。例如: ```yaml spring: security: oauth2: client: registration: my-client-id: client-id: your-client-id client-secret: your-client-secret provider: your-authorization-server ``` 请替换`my-client-id`,`your-client-id`,`your-client-secret`和`your-authorization-server`为您的实际值。 3. 在您的代码中,您可以使用`RestTemplate`或`WebClient`来发送请求并获取token。以下是使用`RestTemplate`的示例代码: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationCodeGrantRequest; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.net.URI; import java.util.Collections; @RestController public class OAuth2Controller { private final ClientRegistrationRepository clientRegistrationRepository; private final OAuth2AuthorizedClientManager authorizedClientManager; public OAuth2Controller(ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientManager authorizedClientManager) { this.clientRegistrationRepository = clientRegistrationRepository; this.authorizedClientManager = authorizedClientManager; } @GetMapping("/oauth2/token") public String getToken() { // 获取ClientRegistration ClientRegistration clientRegistration = clientRegistrationRepository.findByRegistrationId("my-client-id"); // 构建OAuth2AuthorizationRequest OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() .clientId(clientRegistration.getClientId()) .authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri()) .redirectUri(URI.create("http://localhost:8080/oauth2/callback")) .scopes(clientRegistration.getScopes()) .state("state") .build(); // 构建OAuth2AuthorizationResponse OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.success("authorization-code") .redirectUri("http://localhost:8080/oauth2/callback") .state("state") .build(); // 构建OAuth2AuthorizationCodeGrantRequest OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest = new OAuth2AuthorizationCodeGrantRequest( clientRegistration, authorizationRequest, authorizationResponse); // 构建OAuth2AuthorizedClientProvider OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder() .authorizationCode() .build(); // 获取OAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizationCodeGrantRequest); // 构建请求头 HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setBearerAuth(authorizedClient.getAccessToken().getTokenValue()); // 发送请求并获取响应 RequestEntity<Void> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create("http://api.example.com/resource")); ResponseEntity<String> responseEntity = new RestTemplate().exchange(requestEntity, String.class); return responseEntity.getBody(); } } ``` 请确保替换`my-client-id`和`http://localhost:8080/oauth2/callback`为实际值。在这个例子中,我们模拟了一个授权码授权流程来获取token,并使用token发送请求并获取资源的响应。 这只是一个简单的示例,实际的实现可能会有所不同,具体取决于您的认证服务器和需求。您可以根据您的情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值