fix: oauth2 login callback exception
This commit is contained in:
parent
02c1b5e20f
commit
444dee0b25
|
@ -1,7 +1,9 @@
|
||||||
package com.databasir.api;
|
package com.databasir.api;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class IndexController {
|
public class IndexController {
|
||||||
|
@ -11,4 +13,8 @@ public class IndexController {
|
||||||
return "index.html";
|
return "index.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||||
|
public String handleResourceNotFoundException() {
|
||||||
|
return "/index.html";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
.and()
|
.and()
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
.antMatchers("/login", Routes.Login.REFRESH_ACCESS_TOKEN).permitAll()
|
.antMatchers("/login", Routes.Login.REFRESH_ACCESS_TOKEN).permitAll()
|
||||||
.antMatchers("/oauth2/apps", "/oauth2/failure", "/oauth2/authorization/*", "/oauth2/login/*")
|
.antMatchers("/oauth2/apps", "/oauth2/failure", "/oauth2/authorization/*",
|
||||||
|
"/oauth2/login/*", "/login/oauth2/*")
|
||||||
.permitAll()
|
.permitAll()
|
||||||
.antMatchers("/", "/*.html", "/js/**", "/css/**", "/img/**", "/*.ico").permitAll()
|
.antMatchers("/", "/*.html", "/js/**", "/css/**", "/img/**", "/*.ico").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
|
|
|
@ -3,10 +3,15 @@ package com.databasir.api.config;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||||
|
import org.springframework.boot.web.server.ErrorPage;
|
||||||
|
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||||
|
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
import org.springframework.data.web.config.EnableSpringDataWebSupport;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
@ -22,6 +27,16 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
.allowedMethods("GET", "POST", "DELETE", "PATCH", "PUT");
|
.allowedMethods("GET", "POST", "DELETE", "PATCH", "PUT");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addViewControllers(ViewControllerRegistry registry) {
|
||||||
|
registry.addViewController("/notFound").setViewName("forward:/index.html");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
|
||||||
|
return container -> container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound"));
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
|
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
|
||||||
final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
|
@ -43,10 +43,10 @@ public class GithubOpenAuthHandler implements OpenAuthHandler {
|
||||||
public OAuthProcessResult process(OauthAppPojo app, Map<String, String[]> requestParams) {
|
public OAuthProcessResult process(OauthAppPojo app, Map<String, String[]> requestParams) {
|
||||||
String clientId = app.getClientId();
|
String clientId = app.getClientId();
|
||||||
String clientSecret = app.getClientSecret();
|
String clientSecret = app.getClientSecret();
|
||||||
String baseUrl = app.getResourceUrl();
|
String authUrl = app.getAuthUrl();
|
||||||
|
|
||||||
String code = requestParams.get("code")[0];
|
String code = requestParams.get("code")[0];
|
||||||
JsonNode tokenNode = githubRemoteService.getToken(baseUrl, clientId, clientSecret, code)
|
JsonNode tokenNode = githubRemoteService.getToken(authUrl, clientId, clientSecret, code)
|
||||||
.get("access_token");
|
.get("access_token");
|
||||||
if (tokenNode == null) {
|
if (tokenNode == null) {
|
||||||
throw new DatabasirAuthenticationException(DomainErrors.NETWORK_ERROR.exception());
|
throw new DatabasirAuthenticationException(DomainErrors.NETWORK_ERROR.exception());
|
||||||
|
@ -55,8 +55,9 @@ public class GithubOpenAuthHandler implements OpenAuthHandler {
|
||||||
if (StringUtils.isBlank(accessToken)) {
|
if (StringUtils.isBlank(accessToken)) {
|
||||||
throw new CredentialsExpiredException("授权失效,请重新登陆");
|
throw new CredentialsExpiredException("授权失效,请重新登陆");
|
||||||
}
|
}
|
||||||
|
String resourceUrl = app.getResourceUrl();
|
||||||
String email = null;
|
String email = null;
|
||||||
for (JsonNode node : githubRemoteService.getEmail(baseUrl, accessToken)) {
|
for (JsonNode node : githubRemoteService.getEmail(resourceUrl, accessToken)) {
|
||||||
if (node.get("primary").asBoolean()) {
|
if (node.get("primary").asBoolean()) {
|
||||||
email = node.get("email").asText();
|
email = node.get("email").asText();
|
||||||
}
|
}
|
||||||
|
@ -64,7 +65,7 @@ public class GithubOpenAuthHandler implements OpenAuthHandler {
|
||||||
if (StringUtils.isBlank(email)) {
|
if (StringUtils.isBlank(email)) {
|
||||||
throw new CredentialsExpiredException("授权失效,请重新登陆");
|
throw new CredentialsExpiredException("授权失效,请重新登陆");
|
||||||
}
|
}
|
||||||
JsonNode profile = githubRemoteService.getProfile(baseUrl, accessToken);
|
JsonNode profile = githubRemoteService.getProfile(resourceUrl, accessToken);
|
||||||
String nickname = profile.get("name").asText();
|
String nickname = profile.get("name").asText();
|
||||||
String avatar = profile.get("avatar_url").asText();
|
String avatar = profile.get("avatar_url").asText();
|
||||||
OAuthProcessResult result = new OAuthProcessResult();
|
OAuthProcessResult result = new OAuthProcessResult();
|
||||||
|
|
|
@ -37,6 +37,7 @@ public class GitlabOpenAuthHandler implements OpenAuthHandler {
|
||||||
.queryParam("redirect_uri", redirectUri)
|
.queryParam("redirect_uri", redirectUri)
|
||||||
.queryParam("response_type", "code")
|
.queryParam("response_type", "code")
|
||||||
.queryParam("state", redirectUri)
|
.queryParam("state", redirectUri)
|
||||||
|
.queryParam("scope", "read_user")
|
||||||
.encode()
|
.encode()
|
||||||
.build()
|
.build()
|
||||||
.toUriString();
|
.toUriString();
|
||||||
|
|
Loading…
Reference in New Issue