Spring 自定义集中式授权服务器 (oauth) - 验证和自定义时出错
Spring custom centralized authorization server (oauth) - Error authenticating and customizing
我对 spring 和 OAuth 都很陌生,这让我坚持要解决这个令人兴奋的问题!
据我所知,我需要做的是创建一个带有 oauth 和开放 ID 的集中式授权服务器。
我们有一个中央用户数据库,我们希望我们所有的应用程序都调用授权服务器、登录、自动批准(因为唯一的流量应该来自我们的服务器)并根据他们传递的 URI。
理想情况下,我们会为他们提供授权令牌和 ID 令牌(开放 ID),以便他们获得有关已登录用户的信息。
所以我正在努力设置一个 Spring 引导应用程序,它充当授权服务器。到目前为止,我看到了登录页面 - 但每次登录尝试都以以下错误结束:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
这是我目前对该服务器的了解:
SpringBootServletInitializer(应用程序初始化程序)- com.company
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
AuthorizationServerConfigurerAdapter(授权服务器)- com.company.config
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my_client")
.secret("my_secret")
.autoApprove(true)
.authorizedGrantTypes("authorization_code", "refresh_token", "password")
.scopes("openid")
.accessTokenValiditySeconds(600);
}
}
WebSecurityConfigurerAdapter(安全)- com.company.config
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.and()
.httpBasic().disable()
.anonymous().disable()
.authorizeRequests().anyRequest().authenticated();
// disabled basic auth and configured to use dafault Spring Security form login.
}
}
所以问题:
- 那个错误是什么,为什么我会看到它?
- 如何自定义我看到的登录页面?我正在拼命地查看 Spring 文档,我 lost/overwhelmed。我的 属性 文件之外的资源文件夹中没有任何内容。
来自 spring 的错误被启用以映射到错误页面,那是因为存在异常。
您可以指定自己的登录页面如下:
.formLogin()
.loginPage("/login.html") //your custom login page
.defaultSuccessUrl("/homepage.html", true) //welcome page after login success
.failureUrl("/login.html?error=true") // when AccessDenied happens
我对 spring 和 OAuth 都很陌生,这让我坚持要解决这个令人兴奋的问题!
据我所知,我需要做的是创建一个带有 oauth 和开放 ID 的集中式授权服务器。
我们有一个中央用户数据库,我们希望我们所有的应用程序都调用授权服务器、登录、自动批准(因为唯一的流量应该来自我们的服务器)并根据他们传递的 URI。
理想情况下,我们会为他们提供授权令牌和 ID 令牌(开放 ID),以便他们获得有关已登录用户的信息。
所以我正在努力设置一个 Spring 引导应用程序,它充当授权服务器。到目前为止,我看到了登录页面 - 但每次登录尝试都以以下错误结束:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
这是我目前对该服务器的了解:
SpringBootServletInitializer(应用程序初始化程序)- com.company
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
AuthorizationServerConfigurerAdapter(授权服务器)- com.company.config
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my_client")
.secret("my_secret")
.autoApprove(true)
.authorizedGrantTypes("authorization_code", "refresh_token", "password")
.scopes("openid")
.accessTokenValiditySeconds(600);
}
}
WebSecurityConfigurerAdapter(安全)- com.company.config
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.and()
.httpBasic().disable()
.anonymous().disable()
.authorizeRequests().anyRequest().authenticated();
// disabled basic auth and configured to use dafault Spring Security form login.
}
}
所以问题:
- 那个错误是什么,为什么我会看到它?
- 如何自定义我看到的登录页面?我正在拼命地查看 Spring 文档,我 lost/overwhelmed。我的 属性 文件之外的资源文件夹中没有任何内容。
来自 spring 的错误被启用以映射到错误页面,那是因为存在异常。
您可以指定自己的登录页面如下:
.formLogin()
.loginPage("/login.html") //your custom login page
.defaultSuccessUrl("/homepage.html", true) //welcome page after login success
.failureUrl("/login.html?error=true") // when AccessDenied happens