- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Spring Security 来验证用户身份。
问题是动态更新权限的最佳方法是什么?我想根据请求更新它,现在我只需在用户登录系统后执行一次。
我有基于经理的应用程序,因此管理员可以随时决定用户可以做什么,并删除/添加角色。这种方法的问题在于,用户只有在注销并重新登录后才会获得新的权限集。
我知道我可以使用
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> authorities = Lists.newArrayList();
userDao.getAuthorities(authorities, user);
Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
主要问题是什么时候做这件事合适?框架命中 Controller 或拦截器之前的一些过滤器链?它有多安全?线程安全?
假设我将它放入拦截器中,当我在一个请求中更新 SecurityContextHolder 时,另一个请求读取它 - 会发生什么?
只是快速草稿
public class VerifyAccessInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> authorities = Lists.newArrayList();
userDao.getAuthorities(authorities, user);
Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
}
}
最佳答案
如果我错了,请纠正我。
从您的问题中可以看出,您显然希望基于请求进行Authority
更改。理想情况下,管理员将有一个不同的 UI,他可以在其中删除/添加权限。这些更改必须近乎实时地反射(reflect)在任何登录的用户 session 中。
目前,您提供的代码片段是您可以做到这一点的唯一方法。
解答您的疑虑。
主要问题是什么时候做这件事合适?
如果您希望它仅应用于登录的用户,那么您必须将其放在拦截器中,因为它只会在 Spring 安全过滤器链之后应用。
Some filter chain before framework hit controller or interceptor?
是的,在您的请求到达 Controller 或拦截器之前,将首先调用 DelegatingFilterProxy 和 FilterChainProxy。
And how safe it is? Thread safe?
是的,如果您使用具有默认设置的SecurityContextHolder
,它将使用线程安全的ThreadLocalSecurityContextHolderStrategy
。
由于所有请求都必须经过拦截器,因此性能会受到影响。而且,由于只有在权限更改得更好时,您才需要更改权限,以便在重新设置身份验证
public class VerifyAccessInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> authorities = Lists.newArrayList();
userDao.getAuthorities(authorities, user);
// If contents of auth.getAuthorities() equals authorities then no need to re-set.
Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities);
SecurityContextHolder.getContext().setAuthentication(newAuth);
}
}
关于java - 通过 Spring SecurityContextHolder 更新权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311922/
回答这个问题securitycontextholder-session-or-request-bound 。我正在使用此示例 how-to-customAuthenticationManager 实现
我使用 SecurityContextHolder 和自定义 UserDetailsService 从 SecurityContextHolder 获取 UserDetails: Object o
我有登录参数 1.userName 2.password 3.companyId 我使用以下代码获得了用户名和密码 Authentication auth = SecurityContextHold
我正在尝试在Spring Boot应用程序中使用Spring Security添加Facebook授权。目前,我的问题是从Principal提取数据。 这是我的安全配置: public class S
我使用这样的代码进行身份验证: @PreAuthorize("isAnonymous()") @RequestMapping(value = "/login", method = RequestMet
我正在阅读 Spring Security 文档,它说 SecurityContextHolder 为具有特定线程行为的应用程序提供了不同类型的策略。例如 - Swing 应用程序。 我知道在网络应用
我遇到了 SecurityContextHolder.getContext().getAuthentication() 的问题,它是 null。我尝试了很多与注释和示例的组合。 (来自站点的代码在我的
我尝试实现存储所有登录的日志文件。 到目前为止,我在 LoginHandler 中添加了一些代码,但总是收到错误: org.springframework.security.core.userdeta
如何通过将ManualUserDetails扩展org.springframework.security.core.userdetails.UserDetails添加到SecurityContextH
我在我的 Spring Boot 项目中处理安全层时遇到了以下问题: SecurityContextHolder.getContext().getAuthentication() 此代码返回: 匿名用
我正在使用 Spring Security 来验证用户身份。 问题是动态更新权限的最佳方法是什么?我想根据请求更新它,现在我只需在用户登录系统后执行一次。 我有基于经理的应用程序,因此管理员可以随时决
我正在基于一些开源项目和网上找到的一些文档,使用 Spring boot 来实现 JWT。 什么在起作用?我能够生成我的 token ,并且在第一次点击时,当我尝试调用一些安全方法时,我被撤销,这很好
我知道这个问题经常被问到,但也许我有一些特别的事情。我正在尝试对支持 REST(不是 Spring MVC)的 Spring Boot 应用程序进行一些集成测试,出于某种原因 SecurityCont
当我尝试模拟时 SecurityContextHolder.getContext().getAuthentication().getCredentials() 要获得 UserDetails 的 Ma
我创建了一个 @Aspect 类并尝试获取主要对象,例如.. SecurityContextHolder.getContext().getAuthentication() .getPrincipal(
我正在实现 MyUsernamePasswordAuthenticationFilter 扩展默认过滤器。 我没有在其中做任何事情。我只是想在这里获取请求以使其变得简单。 public class M
在我的 spring 应用程序中,我对 Controller 方法有一些方面,我在其中进行一些安全检查。因为我需要更频繁地进行多次检查,所以我将它们包装到“sercurityUtil”类的静态辅助方法
我在我的网络项目中使用 SpringSecurity。我有保存在数据库中的实体用户。我需要使用用户 ID 在 BD 中执行请求,以从该 BD 中的其他表获取任何信息。在 Spring Security
我有两个不同的应用程序,例如 A 和 .两者都使用具有相同配置的 Spring Security。这是我的情况: 我登录到我的 A 应用程序。一切正常。但是,当我在同一浏览器的另一个选项卡中登录我的
我一直在尝试为以下代码行编写测试用例,但我不断收到 java.lang.NullPointerException,我已尝试遵循/复制其他人在此处提出的建议 Unit testing with Spri
我是一名优秀的程序员,十分优秀!