xkcoding/spring-boot-demo · error · RuntimeException
不支持的类型
Error message
不支持的类型
What it means
OauthController.getAuthSource throws RuntimeException('不支持的类型') only when oauthType is blank; for a non-blank but unknown provider, AuthSource.valueOf(type.toUpperCase()) instead throws IllegalArgumentException that escapes uncaught. The explicit message covers the blank-input case, but the more common unknown-provider path is not handled.
Source
Thrown at demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java:81
* 登录成功后的回调
*
* @param oauthType 第三方登录类型
* @param callback 携带返回的信息
* @return 登录成功后的信息
*/
@RequestMapping("/{oauthType}/callback")
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
AuthRequest authRequest = factory.get(getAuthSource(oauthType));
AuthResponse response = authRequest.login(callback);
log.info("【response】= {}", JSONUtil.toJsonStr(response));
return response;
}
private AuthSource getAuthSource(String type) {
if (StrUtil.isNotBlank(type)) {
return AuthSource.valueOf(type.toUpperCase());
} else {
throw new RuntimeException("不支持的类型");
}
}
}
View on GitHub (pinned to 87a142f960)
Solutions
- Validate oauthType against factory.oauthList() before calling AuthSource.valueOf
- Catch IllegalArgumentException and return HTTP 400 listing the supported providers
- Guard the blank case explicitly with a 400 instead of a raw RuntimeException
Example fix
// before
private AuthSource getAuthSource(String type) {
if (StrUtil.isNotBlank(type)) {
return AuthSource.valueOf(type.toUpperCase());
}
throw new RuntimeException('不支持的类型');
}
// after
private AuthSource getAuthSource(String type) {
if (StrUtil.isBlank(type) || !factory.oauthList().contains(type.toUpperCase())) {
throw new IllegalArgumentException('不支持的类型: ' + type);
}
return AuthSource.valueOf(type.toUpperCase());
} Defensive patterns
Strategy: validation
Validate before calling
List<String> allowed = factory.oauthList();
String upper = oauthType == null ? '' : oauthType.toUpperCase();
if (StrUtil.isBlank(oauthType) || !allowed.contains(upper)) {
return ResponseEntity.badRequest().body('unsupported type, allowed: ' + allowed);
} Try / catch
try { AuthSource src = getAuthSource(oauthType); }
catch (RuntimeException e) { /* '不支持的类型' or IllegalArgumentException */ return ResponseEntity.badRequest().body(e.getMessage()); } Prevention
- Whitelist oauthType against factory.oauthList() before AuthSource.valueOf
- Treat unknown providers as 400, not 500
- Keep the AuthSource enum and justauth version in sync with enabled providers
When it happens
Trigger: GET /oauth/login/{oauthType} or /{oauthType}/callback with an empty path variable, or with a provider name that is not a constant in the justauth AuthSource enum / not registered in the AuthRequestFactory.
Common situations: Hand-edited URLs; a provider disabled in configuration; a typo in the provider name; a justauth version whose AuthSource enum lacks the requested provider.
Related errors
AI-assisted analysis of xkcoding/spring-boot-demo@87a142f960 (2026-08-14).
Data as JSON: /api/errors/fa42500979a027bf.
Report an issue: GitHub.