扩展OAuth2.0自定义接口调用

	@PostMapping("getSsoToken")
    @ApiOperation(value = "获取登录token")
    public Object getSsoToken(HttpServletRequest request, @RequestParam Map<String, String> parameters) {
        SSOUserResponse userInfo = new SSOUserResponse();
        if (StringUtils.isNotBlank(parameters.get("callback"))) {
            // 1,通过callback获取token
            String token = getTokenByCallback(parameters);
            if (StringUtils.isBlank(token)) {
                return Result.failed("通过callback获取用户token失败");
            }
            // 2,通过token获取用户信息
            userInfo = getUserInfoByToken(token);
        }
        if (Objects.isNull(userInfo) || StringUtils.isBlank(userInfo.getAppAccount())) {
            return Result.failed("通过token获取用户信息失败");
        }
        // 3,组装用户信息并通过oauth2认证
        try {
            return getOAuth2AccessToken(request, parameters, userInfo);
        } catch (HttpRequestMethodNotSupportedException e) {
            e.printStackTrace();
            return Result.failed("用户信息无法通过oauth认证,异常认证信息为:" + e.getMessage());
        }
    }

	/**
     * @author: Created by AWU
     * @date: Created in 2023/06/13 16:53
     */
    private Object getOAuth2AccessToken(HttpServletRequest request, Map<String, String> parameters,
                                        SSOUserResponse userInfo) throws HttpRequestMethodNotSupportedException {
        SysUser sysUser = userService.selectByUsername(userInfo.getAppAccount());
        if (Objects.isNull(sysUser) || StringUtils.isBlank(sysUser.getUsername())) {
            sysUser = createNewUserHaveDefaultRoles(userInfo);
        }
        if (Objects.isNull(sysUser)) {
            return Result.failed("新建用户信息由于单点登录用户数据获取失败而无法成功");
        }
        // 4, 用户信息判空处理,密码是否需要解密等
        parameters.put("username", sysUser.getUsername());
        parameters.put("password", sysUser.getPassword());
        parameters.put("grant_type", "password");
        UsernamePasswordAuthenticationToken authenticationToken = convert(request);
        if (Objects.isNull(authenticationToken)) {
            return Result.failed("验证失败");
        }
        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(authenticationToken.getName());
        if (Objects.isNull(clientDetails)) {
            return Result.failed("验证失败");
        }
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        return tokenEndpoint.postAccessToken(authenticationToken, parameters);
    }

	/**
     * @author: Created by AWU
     * @date: Created in 2023/06/13 15:37
     */
    private UsernamePasswordAuthenticationToken convert(HttpServletRequest request) {
        String header = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (header == null) {
            return null;
        }
        header = header.trim();
        if (!org.springframework.util.StringUtils.startsWithIgnoreCase(header, AUTHENTICATION_SCHEME_BASIC)) {
            return null;
        }
        if (header.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) {
            throw new BadCredentialsException("Empty basic authentication token");
        }
        byte[] base64Token = header.substring(6).getBytes(StandardCharsets.UTF_8);
        byte[] decoded = decode(base64Token);
        String token = new String(decoded, StandardCharsets.UTF_8);
        int delim = token.indexOf(":");
        if (delim == -1) {
            throw new BadCredentialsException("Invalid basic authentication token");
        }
        return new UsernamePasswordAuthenticationToken(token.substring(0, delim), token.substring(delim + 1), null);

    }

	/**
     * @author: Created by AWU
     * @date: Created in 2023/06/13 15:38
     */
    private byte[] decode(byte[] base64Token) {
        try {
            return Base64.getDecoder().decode(base64Token);
        } catch (IllegalArgumentException ex) {
            throw new BadCredentialsException("Failed to decode basic authentication token");
        }
    }

end 转载记得标明来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值