OAuth2 SSO with Spring Boot without the authorization screen
OAuth2 SSO with Spring Boot without the authorization screen
我有 资源、授权 和 _ui 个使用 Spring Boot 1.5.3、OAuth2 和 MongoDB 编写的应用程序。
资源将从移动应用程序和几个 Web 应用程序(一个供普通用户使用,另一个供管理员使用)访问。这些应用程序 quite 类似于 Dave Syer 的 guides 中的 samples。不同之处在于,用户存储在数据库中,而客户端存储在授权服务器资源文件夹中的 xml 文件中。
我正在为 Web 用户的登录体验而苦苦挣扎。在基于 JWT 的 OAuth 应用程序的 guides 之后,在登录页面之后,用户被重定向到授权屏幕,这不是所需的行为。即,我不希望我的授权服务器询问用户是否信任我的 Web 应用程序来访问其资源。相反,我希望用户在登录后立即重定向到 ui 页面,正如人们所期望的那样。
我发现 this project on GitHub(非常类似于 guide 中的应用程序)它的行为完全符合我的要求,但是一旦我开始通过添加我的身份验证和授权实现来自定义它,它恢复到使用授权屏幕。显然,我遗漏了什么,但我无法弄清楚到底是什么。
authorization/src/main/resourcs/application.yml
security:
oauth2:
client:
client-id: trusted-app
client-secret: secret
scope: read, write
auto-approve-scopes: .*
authorization:
check-token-access: permitAll()
server:
port: 9999
context-path: /uaa
mongo:
db:
name: myappname
authorization/src/main/resourcs/client-details.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2.xsd">
<oauth:client-details-service id="client-details-service">
<!-- Web Application clients -->
<oauth:client
client-id="trusted-app"
secret="secret"
authorized-grant-types="authorization_code, password,refresh_token"
authorities="ROLE_WEB, ROLE_TRUSTED_CLIENT"
access-token-validity="${oauth.token.access.expiresInSeconds}"
refresh-token-validity="${oauth.token.refresh.expiresInSeconds}"/>
</oauth:client-details-service>
</beans>
authorization/src/main/java/AuthorizationApplication.java
@SpringBootApplication
@RestController
public class AuthorizationApplication extends AuthorizationServerConfigurerAdapter {
@RequestMapping("/user")
@ResponseBody
public Principal user(Principal user) {
return user;
}
@Configuration
static class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("login").setViewName("login");
registry.addViewController("/").setViewName("index");
}
}
@Configuration
@Order(-20)
static class LoginConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers()
.antMatchers("/", "/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests()
.anyRequest().authenticated();
}
}
@Configuration
@EnableAuthorizationServer
@ImportResource({"classpath*:client-details.xml"})
protected static class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Resource(name="client-details-service")
private ClientDetailsService clientDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
return converter;
}
}
@Bean
PasswordEncoder passwordEncoder(){
return new StandardPasswordEncoder();
}
public static void main(String[] args) {
SpringApplication.run(AuthorizationApplication.class, args);
}
}
authorization/src/main/java/mypackage/UserService.java
@Service
public class UserService implements UserDetailsService {
private UserAccountRepository userAccountRepository;
@Autowired
public UserService(UserAccountRepository userAccountRepository){
this.userAccountRepository = userAccountRepository;
}
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
UserAccount userAccount = userAccountRepository.findByEmail(s);
if (userAccount != null) {
return userAccount;
} else {
throw new UsernameNotFoundException("could not find the user '" + s + "'");
}
}
}
ui/src/main/resources/application.yml
auth-server: http://localhost:9999/uaa
server:
port: 8080
spring:
aop:
proxy-target-class: true
security:
oauth2:
client:
clientId: trusted-app
clientSecret: secret
access-token-uri: ${auth-server}/oauth/token
user-authorization-uri: ${auth-server}/oauth/authorize
scope: read, write
resource:
token-info-uri: ${auth-server}/oauth/check_token
ui/src/main/java/UiApplication.java
@SpringBootApplication
@EnableOAuth2Sso
public class UiApplication extends WebSecurityConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(UiApplication.class, args);
}
@Bean
OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, OAuth2ProtectedResourceDetails details) {
return new OAuth2RestTemplate(details, oauth2ClientContext);
}
}
来自 http://www.springframework.org/schema/security/spring-security-oauth2.xsd 元素 client-details-service > complexType client > attribute autoaprove
Scopes or scope patterns that are autoapproved (comma-separated), or
just "true" to autoapprove all.
只需将 autoapprove="true"
属性添加到 client-details.xml
中的受信任应用。这样 authserver 将不会请求用户确认访问资源。
是如何在 Java 配置中直接实现此行为的示例。
另外,如果你的客户端是由数据库提供的,实际上是DataSource
,那么相关客户端的AUTOAPPROVE
列应该设置为table中的'true' OAUTH_CLIENT_DETAILS
.
我有 资源、授权 和 _ui 个使用 Spring Boot 1.5.3、OAuth2 和 MongoDB 编写的应用程序。
资源将从移动应用程序和几个 Web 应用程序(一个供普通用户使用,另一个供管理员使用)访问。这些应用程序 quite 类似于 Dave Syer 的 guides 中的 samples。不同之处在于,用户存储在数据库中,而客户端存储在授权服务器资源文件夹中的 xml 文件中。
我正在为 Web 用户的登录体验而苦苦挣扎。在基于 JWT 的 OAuth 应用程序的 guides 之后,在登录页面之后,用户被重定向到授权屏幕,这不是所需的行为。即,我不希望我的授权服务器询问用户是否信任我的 Web 应用程序来访问其资源。相反,我希望用户在登录后立即重定向到 ui 页面,正如人们所期望的那样。
我发现 this project on GitHub(非常类似于 guide 中的应用程序)它的行为完全符合我的要求,但是一旦我开始通过添加我的身份验证和授权实现来自定义它,它恢复到使用授权屏幕。显然,我遗漏了什么,但我无法弄清楚到底是什么。
authorization/src/main/resourcs/application.yml
security:
oauth2:
client:
client-id: trusted-app
client-secret: secret
scope: read, write
auto-approve-scopes: .*
authorization:
check-token-access: permitAll()
server:
port: 9999
context-path: /uaa
mongo:
db:
name: myappname
authorization/src/main/resourcs/client-details.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2.xsd">
<oauth:client-details-service id="client-details-service">
<!-- Web Application clients -->
<oauth:client
client-id="trusted-app"
secret="secret"
authorized-grant-types="authorization_code, password,refresh_token"
authorities="ROLE_WEB, ROLE_TRUSTED_CLIENT"
access-token-validity="${oauth.token.access.expiresInSeconds}"
refresh-token-validity="${oauth.token.refresh.expiresInSeconds}"/>
</oauth:client-details-service>
</beans>
authorization/src/main/java/AuthorizationApplication.java
@SpringBootApplication
@RestController
public class AuthorizationApplication extends AuthorizationServerConfigurerAdapter {
@RequestMapping("/user")
@ResponseBody
public Principal user(Principal user) {
return user;
}
@Configuration
static class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("login").setViewName("login");
registry.addViewController("/").setViewName("index");
}
}
@Configuration
@Order(-20)
static class LoginConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers()
.antMatchers("/", "/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests()
.anyRequest().authenticated();
}
}
@Configuration
@EnableAuthorizationServer
@ImportResource({"classpath*:client-details.xml"})
protected static class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Resource(name="client-details-service")
private ClientDetailsService clientDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
return converter;
}
}
@Bean
PasswordEncoder passwordEncoder(){
return new StandardPasswordEncoder();
}
public static void main(String[] args) {
SpringApplication.run(AuthorizationApplication.class, args);
}
}
authorization/src/main/java/mypackage/UserService.java
@Service
public class UserService implements UserDetailsService {
private UserAccountRepository userAccountRepository;
@Autowired
public UserService(UserAccountRepository userAccountRepository){
this.userAccountRepository = userAccountRepository;
}
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
UserAccount userAccount = userAccountRepository.findByEmail(s);
if (userAccount != null) {
return userAccount;
} else {
throw new UsernameNotFoundException("could not find the user '" + s + "'");
}
}
}
ui/src/main/resources/application.yml
auth-server: http://localhost:9999/uaa
server:
port: 8080
spring:
aop:
proxy-target-class: true
security:
oauth2:
client:
clientId: trusted-app
clientSecret: secret
access-token-uri: ${auth-server}/oauth/token
user-authorization-uri: ${auth-server}/oauth/authorize
scope: read, write
resource:
token-info-uri: ${auth-server}/oauth/check_token
ui/src/main/java/UiApplication.java
@SpringBootApplication
@EnableOAuth2Sso
public class UiApplication extends WebSecurityConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(UiApplication.class, args);
}
@Bean
OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, OAuth2ProtectedResourceDetails details) {
return new OAuth2RestTemplate(details, oauth2ClientContext);
}
}
来自 http://www.springframework.org/schema/security/spring-security-oauth2.xsd 元素 client-details-service > complexType client > attribute autoaprove
Scopes or scope patterns that are autoapproved (comma-separated), or just "true" to autoapprove all.
只需将 autoapprove="true"
属性添加到 client-details.xml
中的受信任应用。这样 authserver 将不会请求用户确认访问资源。
另外,如果你的客户端是由数据库提供的,实际上是DataSource
,那么相关客户端的AUTOAPPROVE
列应该设置为table中的'true' OAUTH_CLIENT_DETAILS
.