commit
03507b8cd4
2 changed files with 183 additions and 1 deletions
@ -0,0 +1,182 @@ |
|||||||
|
package org.springblade.core.oauth2.endpoint; |
||||||
|
|
||||||
|
import com.wf.captcha.SpecCaptcha; |
||||||
|
import io.swagger.v3.oas.annotations.Operation; |
||||||
|
import io.swagger.v3.oas.annotations.Parameter; |
||||||
|
import io.swagger.v3.oas.annotations.enums.ParameterIn; |
||||||
|
import io.swagger.v3.oas.annotations.media.Schema; |
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag; |
||||||
|
import lombok.Generated; |
||||||
|
import org.springblade.core.cache.utils.CacheUtil; |
||||||
|
import org.springblade.core.jwt.JwtUtil; |
||||||
|
import org.springblade.core.jwt.props.JwtProperties; |
||||||
|
import org.springblade.core.launch.props.BladeProperties; |
||||||
|
import org.springblade.core.log.annotation.ApiLog; |
||||||
|
import org.springblade.core.oauth2.exception.OAuth2Exception; |
||||||
|
import org.springblade.core.oauth2.granter.TokenGranter; |
||||||
|
import org.springblade.core.oauth2.granter.TokenGranterFactory; |
||||||
|
import org.springblade.core.oauth2.handler.AuthorizationHandler; |
||||||
|
import org.springblade.core.oauth2.handler.TokenHandler; |
||||||
|
import org.springblade.core.oauth2.provider.OAuth2Request; |
||||||
|
import org.springblade.core.oauth2.provider.OAuth2Response; |
||||||
|
import org.springblade.core.oauth2.provider.OAuth2Token; |
||||||
|
import org.springblade.core.oauth2.provider.OAuth2Validation; |
||||||
|
import org.springblade.core.oauth2.service.OAuth2User; |
||||||
|
import org.springblade.core.oauth2.utils.OAuth2ExceptionUtil; |
||||||
|
import org.springblade.core.oauth2.utils.OAuth2LogUtil; |
||||||
|
import org.springblade.core.redis.cache.BladeRedis; |
||||||
|
import org.springblade.core.secure.BladeUser; |
||||||
|
import org.springblade.core.secure.utils.AuthUtil; |
||||||
|
import org.springblade.core.tool.support.Kv; |
||||||
|
import org.springframework.http.ResponseEntity; |
||||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
import java.time.Duration; |
||||||
|
import java.util.UUID; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@Tag( |
||||||
|
name = "用户授权认证", |
||||||
|
description = "1 - OAuth2授权认证端点" |
||||||
|
) |
||||||
|
public class OAuth2TokenEndPoint { |
||||||
|
private final BladeRedis bladeRedis; |
||||||
|
private final JwtProperties jwtProperties; |
||||||
|
private final BladeProperties bladeProperties; |
||||||
|
private final TokenGranterFactory granterFactory; |
||||||
|
private final AuthorizationHandler authorizationHandler; |
||||||
|
private final TokenHandler tokenHandler; |
||||||
|
|
||||||
|
@PostMapping({"/oauth/token"}) |
||||||
|
@Operation( |
||||||
|
summary = "获取Token", |
||||||
|
description = "OAuth2认证接口", |
||||||
|
parameters = {@Parameter( |
||||||
|
in = ParameterIn.QUERY, |
||||||
|
name = "username", |
||||||
|
description = "账号", |
||||||
|
schema = @Schema( |
||||||
|
type = "string" |
||||||
|
) |
||||||
|
), @Parameter( |
||||||
|
in = ParameterIn.QUERY, |
||||||
|
name = "password", |
||||||
|
description = "密码", |
||||||
|
schema = @Schema( |
||||||
|
type = "string" |
||||||
|
) |
||||||
|
), @Parameter( |
||||||
|
in = ParameterIn.QUERY, |
||||||
|
name = "grant_type", |
||||||
|
description = "授权类型", |
||||||
|
schema = @Schema( |
||||||
|
type = "string" |
||||||
|
) |
||||||
|
), @Parameter( |
||||||
|
in = ParameterIn.QUERY, |
||||||
|
name = "refresh_token", |
||||||
|
description = "刷新token", |
||||||
|
schema = @Schema( |
||||||
|
type = "string" |
||||||
|
) |
||||||
|
), @Parameter( |
||||||
|
in = ParameterIn.QUERY, |
||||||
|
name = "scope", |
||||||
|
description = "权限范围", |
||||||
|
schema = @Schema( |
||||||
|
type = "string" |
||||||
|
) |
||||||
|
)} |
||||||
|
) |
||||||
|
@ApiLog(value = "登录") |
||||||
|
public ResponseEntity<Kv> token() { |
||||||
|
OAuth2Request request = OAuth2Request.create().buildArgs(); |
||||||
|
OAuth2Validation preValidation = this.authorizationHandler.preValidation(request); |
||||||
|
if (!preValidation.isSuccess()) { |
||||||
|
this.authorizationHandler.preFailure(request, preValidation); |
||||||
|
return ResponseEntity.ok(OAuth2Response.create().ofFailure(preValidation.getCode(), preValidation.getMessage())); |
||||||
|
} else { |
||||||
|
TokenGranter tokenGranter = this.granterFactory.create(request.getGrantType()); |
||||||
|
|
||||||
|
OAuth2User user; |
||||||
|
try { |
||||||
|
user = tokenGranter.user(request); |
||||||
|
} catch (OAuth2Exception var8) { |
||||||
|
OAuth2LogUtil.logOAuth2Exception(var8, request, this.bladeProperties.isProd()); |
||||||
|
this.authorizationHandler.preFailure(request, OAuth2Validation.create().setSuccess(false).setCode(var8.getExceptionCode().getCode()).setMessage(var8.getMessage())); |
||||||
|
return ResponseEntity.ok(OAuth2Response.create().ofFailure(var8.getExceptionCode().getCode(), var8.getMessage())); |
||||||
|
} |
||||||
|
|
||||||
|
OAuth2Validation authValidation = this.authorizationHandler.authValidation(user, request); |
||||||
|
if (!authValidation.isSuccess()) { |
||||||
|
this.authorizationHandler.authFailure(user, request, authValidation); |
||||||
|
OAuth2ExceptionUtil.throwFromCode(authValidation.getCode()); |
||||||
|
} |
||||||
|
|
||||||
|
OAuth2Token token = tokenGranter.token(user, request); |
||||||
|
OAuth2Token enhanceToken = this.tokenHandler.enhance(user, token, request); |
||||||
|
this.authorizationHandler.authSuccessful(user, request); |
||||||
|
return ResponseEntity.ok(enhanceToken.getArgs()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping({"/oauth/logout"}) |
||||||
|
@Operation( |
||||||
|
summary = "退出登录" |
||||||
|
) |
||||||
|
public ResponseEntity<Kv> logout() { |
||||||
|
BladeUser user = AuthUtil.getUser(); |
||||||
|
if (user != null && this.jwtProperties.getState()) { |
||||||
|
OAuth2Request request = OAuth2Request.create().buildHeaderArgs(); |
||||||
|
String token = JwtUtil.getToken(request.getToken()); |
||||||
|
JwtUtil.removeAccessToken(user.getTenantId(), user.getClientId(), String.valueOf(user.getUserId()), token); |
||||||
|
JwtUtil.removeRefreshToken(user.getTenantId(), user.getClientId(), String.valueOf(user.getUserId()), token); |
||||||
|
} |
||||||
|
|
||||||
|
return ResponseEntity.ok(OAuth2Response.create().ofSuccessful("退出登录成功")); |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping({"/oauth/captcha"}) |
||||||
|
@Operation( |
||||||
|
summary = "获取验证码" |
||||||
|
) |
||||||
|
public ResponseEntity<Kv> captcha() { |
||||||
|
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); |
||||||
|
String verCode = specCaptcha.text().toLowerCase(); |
||||||
|
String key = UUID.randomUUID().toString(); |
||||||
|
this.bladeRedis.setEx("blade:auth::blade:captcha:" + key, verCode, Duration.ofMinutes(30L)); |
||||||
|
return ResponseEntity.ok(OAuth2Response.create().ofSuccessful("获取验证码成功").set("key", key).set("image", specCaptcha.toBase64())); |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping({"/oauth/clear-cache"}) |
||||||
|
@Operation( |
||||||
|
summary = "清除缓存" |
||||||
|
) |
||||||
|
public ResponseEntity<Kv> clearCache() { |
||||||
|
CacheUtil.clear("blade:biz"); |
||||||
|
CacheUtil.clear("blade:user"); |
||||||
|
CacheUtil.clear("blade:dict"); |
||||||
|
CacheUtil.clear("blade:flow"); |
||||||
|
CacheUtil.clear("blade:sys"); |
||||||
|
CacheUtil.clear("blade:param"); |
||||||
|
CacheUtil.clear("blade:resource"); |
||||||
|
CacheUtil.clear("blade:menu"); |
||||||
|
CacheUtil.clear("blade:dict", Boolean.FALSE); |
||||||
|
CacheUtil.clear("blade:menu", Boolean.FALSE); |
||||||
|
CacheUtil.clear("blade:sys", Boolean.FALSE); |
||||||
|
CacheUtil.clear("blade:param", Boolean.FALSE); |
||||||
|
return ResponseEntity.ok(OAuth2Response.create().ofSuccessful("清除缓存成功")); |
||||||
|
} |
||||||
|
|
||||||
|
@Generated |
||||||
|
public OAuth2TokenEndPoint(final BladeRedis bladeRedis, final JwtProperties jwtProperties, final BladeProperties bladeProperties, final TokenGranterFactory granterFactory, final AuthorizationHandler authorizationHandler, final TokenHandler tokenHandler) { |
||||||
|
this.bladeRedis = bladeRedis; |
||||||
|
this.jwtProperties = jwtProperties; |
||||||
|
this.bladeProperties = bladeProperties; |
||||||
|
this.granterFactory = granterFactory; |
||||||
|
this.authorizationHandler = authorizationHandler; |
||||||
|
this.tokenHandler = tokenHandler; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue