Tag: 弹簧安全

Spring Security:基于客户机权限的安全端点

我目前正在使用Spring Security的OAuth2来实现跨多个微服务的授权。 我们的AuthService使用OAuth2令牌执行所有的身份验证,并可以创建用户。 考虑两个客户端:客户端A和客户端B. 客户端A具有权限: CREATE_USER, CREATE_POST客户端B具有权限: READ_USER (是的,我们可以使用范围,但这只是一个例子!) 目标: 只有拥有权限CREATE_USER客户端A才能被允许创建一个用户。 用户正在通过张贴到/users创建。 问题: 问题是,当我发送一个POST请求到用户A的基本身份验证头的/用户端点时, CREATE_USER权限,因为请求命中了AnonymousAuthenticationFilter ,唯一发现的权限是ROLE_ANONYMOUS ,我收到以下内容: 10:38:34.852 [http-nio-9999-exec-1] DEBUG osswaiFilterSecurityInterceptor – Secure object: FilterInvocation: URL: /users; Attributes: [#oauth2.throwOnError(#oauth2.hasAuthority('CREATE_USER))] 10:38:34.852 [http-nio-9999-exec-1] DEBUG osswaiFilterSecurityInterceptor – Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS 10:38:34.854 [http-nio-9999-exec-1] DEBUG […]

启用S​​pring Security全局方法安全性打破了依赖注入(与Kotlin)

我目前正在为Spring Boot应用程序开发身份验证系统,自从我尝试启用Spring Security的全局方法安全性以来,我一直遇到问题。 Spring Boot应用程序是一个REST API,认证系统有点不标准,所以我建立了一个自定义的认证过滤器,添加到预处理请求的HttpSecurity对象中,调用一个自定义的AuthenticationManager并给出最终的Authentication对象SecurityContextHolder 。 那工作正常! 但是,使用这种方法,我必须直接在我重写的WebSecurityConfigurerAdapter.configure方法中为我的端点设置所有的访问规则,这些方法我觉得不太实际,所以我试图直接在我的控制器中使用Spring Security @PreAuthorize注释代替。 问题是,如果我将@EnableGlobalMethodSecurity(prePostEnabled = true)添加到我的安全配置中,则不会注入控制器中的所有@Autowired属性。 它工作正常,如果我设置prePostEnabled = false或如果我完全删除注释,但当然这不是我想要的。 这里显示了一个stacktrace,它显示了被调用的请求处理程序(以及未被初始化的属性): kotlin.UninitializedPropertyAccessException: lateinit property accountService has not been initialized at com.example.api.module.account.AccountController.addAccountRole(AccountController.kt:105) ~[main/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] at […]