gpt4 book ai didi

java - 使用 Spring Boot 的 OAuth2 SSO,无需授权屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:02 24 4
gpt4 key购买 nike

我有使用 Spring Boot 1.5.3、OAuth2 和 MongoDB 编写的资源、授权和 _ui 应用程序。

将通过移动应用程序以及几个 Web 应用程序(一个用于普通用户,另一个用于管理员)访问这些资源。这些应用程序与samples非常相似。来自 Dave Syer 的指南。不同之处在于用户存储在数据库中,而客户端存储在授权服务器资源文件夹中的 xml 文件中。

我正在努力解决网络用户的登录体验问题。按照基于 JWT 的 OAuth 应用程序的指南,在登录页面之后,用户将被重定向到授权屏幕,这不是所需的行为。即,我不希望我的授权服务器询问用户是否信任我的 Web 应用程序访问其资源。相反,我希望用户在登录后立即重定向到 ui 页面,正如人们所期望的那样。

我找到了this project on GitHub (与指南中的应用程序非常相似),其行为完全符合我的要求,但是一旦我开始通过添加身份验证和授权实现来自定义它,它就会恢复使用授权屏幕。显然,我错过了一些东西,但我无法弄清楚到底是什么。

授权/src/main/resources/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/resources/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);
}

}

授权/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 > 属性 autoaprove

Scopes or scope patterns that are autoapproved (comma-separated), or just "true" to autoapprove all.

只需将 autoapprove="true" 属性添加到 client-details.xml 中的可信应用程序即可。这样authserver就不会请求用户确认访问资源。

Here是如何直接在 Java 配置中实现此行为的示例。

关于java - 使用 Spring Boot 的 OAuth2 SSO,无需授权屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com