- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个带有 OAuth2 安全性的 Spring Boot REST API。
今天,我将 spring-boot-starter-parent
的版本从 1.4.2
升级到了 1.5.2
。
变化让我完全困惑。
以前,我可以使用 Postman 测试我的 REST API。当我的访问 token 不正确或者我没有特定资源的权限时,服务器响应如下:
{
"error": "access_denied",
"error_description": "Access is denied"
}
现在它一直将我重定向到 /login
页面...当我登录时 - 它显示我的资源而没有任何 OAuth2 身份验证...
我试图禁用它,我发现了这个神奇的属性:
security.oauth2.resource.filter-order = 3
这一行关闭了重定向到登录页面。
但是,我的问题是:
我的代码中一些更重要的部分:
pom.xml
<!--- .... -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<!--- .... -->
<spring-security-oauth.version>2.1.0.RELEASE</spring-security-oauth.version>
<!--- .... -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Monitor features -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- Security + OAuth2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${spring-security-oauth.version}</version>
</dependency>
<!--- .... -->
application.properties
#other properties
security.oauth2.resource.filter-order = 3
OAuth2.java
public class OAuth2 {
@EnableAuthorizationServer
@Configuration
@ComponentScan
public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManagerBean;
@Autowired
private UserDetailsService userDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("trusted_client")
.authorizedGrantTypes("password", "refresh_token")
.scopes("read", "write");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean).userDetailsService(userDetailsService);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.allowFormAuthenticationForClients();
}
}
@EnableResourceServer
@Configuration
@ComponentScan
public static class ResourceServer extends ResourceServerConfigurerAdapter {
@Autowired
private RoleHierarchy roleHierarchy;
private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy);
return defaultWebSecurityExpressionHandler;
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().expressionHandler(webExpressionHandler())
.antMatchers("/api/**").hasRole("DEVELOPER");
}
}
}
安全.java
@EnableWebSecurity
@Configuration
@ComponentScan
public class Security extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public JpaAccountDetailsService userDetailsService(AccountsRepository accountsRepository) {
return new JpaAccountDetailsService(accountsRepository);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
最佳答案
好的,我现在明白了。
@Cleto Gadelha 向我指出了非常有用的信息。
但是我认为发行说明很不清楚或者遗漏了一些信息。除了 OAuth2 资源过滤器从 3 更改为 SecurityProperties.ACCESS_OVERRIDE_ORDER - 1
之外,关键信息是默认的 WebSecurityConfigurerAdapter
顺序为 100 (source) .
因此,在 1.5.x 版本之前,OAuth2 资源服务器顺序是 3,它具有更高优先级,然后 WebSecurityConfigurerAdapter
。
发布 1.5.x 后,OAuth2 资源服务器顺序设置为 SecurityProperties.ACCESS_OVERRIDE_ORDER - 1
(我认为是 Integer.MAX_VALUE - 8
)现在肯定较低优先级然后基本 WebSecurityConfigurerAdapter
顺序。
这就是从 1.4.x 迁移到 1.5.x 后为我显示登录页面的原因
因此,更优雅和类 java 风格的解决方案是在 WebSecurityConfigurerAdapter
类上设置 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
关于java - SpringBoot 1.5.x + 安全性 + OAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822875/
我正在尝试构建一个托管在服务器上的 Blazor 应用程序,起点位于一个 razor 页面内。 类似的东西: 我的问题是: 如果 Razor 页面具有授权属性,所有 blazor 代码都不是通过身份
对tomcat中管道和阀门机制不懂的小伙伴,参考本篇文章 领域 目前可知结构,如图所示,下面继续分析 GenericPrincipal类 LoginConfig类 Authenticator接口 在T
我刚刚开始学习 React 中的授权和身份验证,我在使用 JWT 完成我的第一个简单登录系统后编写了这篇文章,因为大多数人都知道您在浏览器中存储了一个 token ,然后将其与保存的进行比较现在,当验
我正在为grails项目寻找安全插件。 Spring安全核心1.2.7.3看起来很棒,但是似乎已经有将近一年没有开发了。有谁知道是这样吗? 还有其他好的插件吗? 我也正在使用mongodb,想知道sp
我有一个 WCF 服务,它使用具有消息安全性和用户名身份验证的 NetTcpBinding。在此之前,我使用 WsHttpBinding 但我切换到 NetTcp,因为我可以使用回调。 我的服务配置如
我正在考虑使用 cakePHP 构建一个 Web 应用程序。我的问题是我必须自己编写多少安全内容来防止(SQL 注入(inject)等)? cakePHP 自己处理什么安全问题,我必须编写什么代码?
任何人都有关于为安全环境强化/配置 TFS 的信息? 最佳答案 TFS 使用 Windows 身份验证,因此它与您的网络一样安全。我建议您还查看有关加强网络安全的资源。 Team Foundation
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我一直想知道 codeigniter 设置有多安全。因为数据库密码等信息存储在主应用程序文件夹的配置文件中,黑客可以检索到这些信息吗?我知道您可以将应用程序文件夹移动到远离 Web 根目录的位置,但如
在客户端使用 PouchDB 访问远程服务器时要遵循的最佳安全实践是什么? https://pouchdb.com/getting-started.html上的例子使用代码与远程服务器同步: var
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在开发一个网络应用程序,用户可以在其中上传文件并执行它们。我的意思是,他们可以上传 html 文件,然后通过单击,他们可以在我的 Web 应用程序内的 iframe 中执行该文件并查看渲染的 ht
试图澄清 -AsPlainText ConvertTo-SecureString 中的参数小命令: -AsPlainText Specifies a plain text string to conv
我正在玩 coreos 和 digitalocean,我想开始允许我的容器之间进行内部通信。 我已经为所有主机设置了私有(private)网络,现在我想确保某些容器只打开到 localhost 和内部
我们有两台机器: 服务器 客户 服务器正在运行 Clojure + Ring + ...标准 ClojureScript webstack。 客户端 = 某些运行 Chorme/Firefox/Saf
让我们有一个函数 foo 和一个类 Bar: fun foo(key: String): String? { // returns string or null } class Bar(x: St
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
确保只有正确的用户才能在 Sharepoint 2007 中看到 Web 部件的最佳做法是什么? 有人向我建议了安全组和受众。 最佳答案 这取决于您是在谈论 Web 部件的呈现还是从 Web 部件库添
我试图说服一个团队,使用 jQuery JSONP 调用与不受信任的第三方可能是不安全的。我正在使用标准 jQuery 代码: $.ajax({ url:unsecureserver+"?js
我有以下ajax调用,它检查用户是否是付费成员(member),如果是,则相应地运行某些功能。这可行,但我担心安全性。如果有人在控制台中更改此 ajax 代码,强制 #button 成功运行功能而无需
我是一名优秀的程序员,十分优秀!