- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有使用 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/
有人可以解释一下 spring-boot-parent 和 spring-boot-starter-parent 之间的区别吗,正如我在下面附加的 GIT HUB 代码链接之一中看到的,他们为 spr
我有与 jersey 框架集成的 Spring Boot 应用程序。 现在,当我尝试运行该应用程序时,它只是停留在 Spring 启动徽标上,之后没有任何 react 。 我也尝试添加 -X ,但徽标
我指的是 Spring Boot 关于 的文档自动配置 和 执行器 模块: 自动配置: Spring Boot AutoConfiguration attempts to automatically
我正在尝试将 apache log4j 集成到我的 Spring boot 应用程序中。这是我的 build.gradle 文件: build.gradle buildscript { rep
使用 Spring Boot Maven 插件的以下命令在生产中启动 Spring Boot 应用程序是否是一个好主意或实践? mvn spring-boot:run 最佳答案 不,这是个坏主意。 您
据我所知,spring boot 和 spring session 为我们提供了一站式自动配置,但是当我的应用程序使用 session redis 和应用程序缓存 redis 时,不是同一个 redi
我希望使用Spring Boot创建一个新的Web应用程序。不幸的是,我的服务器在技术堆栈方面相当有限。它安装了Java 5。 谁能告诉我spring boot是否可以在Java 1.5上运行以及什么
我有3个实体 CarWash(设置Wash) Wash(car_wash_id FK到CarWash) WashComment(wash_id FK到Wash) 有什么办法可以写这个查询 @Qu
我一直在关注this文章。 我正在尝试在Spring-boot应用程序中优雅地处理gRPC错误,的主要目标是能够在gRPC客户端中获取错误状态。 在上面的文章之后,我坚持为异常添加拦截器。如何在Spr
我有一个要使用的自定义log4j布局插件。在IntelliJ中运行或与./gradlew bootRun一起运行时,插件可以正常工作。不使用./gradlew bootJar构建启动jar。 启用-D
我想在给定范围 (5001-5100) 的随机端口上启动 Spring Cloud 应用程序(Spring Boot 1.5.14,Spring Cloud Edgware.SR4)。我知道我们可以使
任何人都可以向我展示或指出不使用 spring boot gradle 插件的 spring boot gradle 项目。 我正在寻找类似不使用 gradle 插件的 spring boot sta
我当时尝试包含上述依赖项之一,但找不到任何区别: spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个响应式(Reactive)休息 Controller
我们一直在为我们的应用程序使用 Springboot 1.X。 现在准备开始一些新的应用程序,想知道我们是应该使用 SpringBoot2.0 还是坚持使用 SpringBoot 1.X? 对一种方式
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
当我在 pom.xml 中添加简单的 spring-boot-starter-data-jpa 依赖项时,在 pom.xml 文件中出现错误。如果我删除该依赖项,则不会再有错误。我不确定为什么会发生这
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
我在网上看了很多关于 spring-boot-devtools 的文章和问题,但仍然无法弄清楚为什么它对我不起作用。每次运行我的应用程序时,我都会得到以下信息: 17:54:28.057 [main]
我正在尝试将现有的 Spring 应用程序移植到 Spring Boot。我不使用 spring-boot-starter-data-solr 启动器,但是我的类路径上有 apache solrj (
(这主要是一个历史问题。Pivotal 建议所有论坛讨论都在 StackOverflow 上进行,这就是我在这里问它的原因。) Spring Boot 项目用来证明将应用程序的类和依赖项从可执行 ja
我是一名优秀的程序员,十分优秀!