mar*_*s82 20 authentication session authorization spring-security
我正在尝试将Spring Security集成到我的Web应用程序中.只要您整合整个身份验证和授权过程,这似乎很容易做到.
但是,身份验证和授权似乎都是如此耦合,以至于我理解如何拆分这些进程并独立于授权进行身份验证是非常耗时的.
身份验证过程在我们的系统外部(基于单点登录),并且无法修改.然而,一旦用户成功完成此过程,它就会加载到会话中,包括角色.
我们试图实现的是将此信息用于Spring Security的授权过程,也就是说,强制它从用户会话中获取角色,而不是通过身份验证提供程序获取角色.
有没有办法实现这个目标?
non*_*ont 22
如果您的身份验证已使用SSO服务完成,那么您应该使用spring security的预身份验证过滤器之一.然后,您可以指定将使用预先验证的用户原则填充GrantedAuthority的UserDetails服务(可能是自定义)
SpringSecurity包括几个预身份验证过滤器,包括J2eePreAuthenticatedProcessingFilter和RequestHeaderPreAuthenticatedProcessingFilter.如果您找不到适合您的方法,那么它也是可能的,而且编写自己的方法并不困难,前提是您知道SSO实现在哪里填充数据.(这取决于课程的实施.)
只需实现Filter接口并在doFilter方法中执行以下操作:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// principal is set in here as a header or parameter. you need to find out
// what it's named to extract it
HttpServletRequest req = (HttpServletRequest) request;
if (SecurityContextHolder.getContext().getAuthentication() == null) {
// in here, get your principal, and populate the auth object with
// the right authorities
Authentication auth = doAuthentication(req);
SecurityContextHolder.getContext().setAuthentication(auth);
}
chain.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30070 次 |
最近记录: |