authentication - spring-security:无需身份验证的授权

标签 authentication session authorization spring-security

我正在尝试将 Spring Security 集成到我的 Web 应用程序中。只要你整合了认证和授权的整个过程,这似乎很容易做到。

但是,身份验证和授权似乎如此耦合,以至于我要理解如何拆分这些过程并独立于授权进行身份验证非常耗时。

身份验证过程在我们的系统外部(基于单点登录),无法修改。然而,一旦用户成功完成了这个过程,它就会被加载到 session 中,包括角色。

我们试图实现的是利用这些信息来进行 Spring Security 的授权过程,也就是说,强制它从用户 session 中获取角色,而不是通过 authentication-provider 来获取它。

有什么办法可以做到这一点?

最佳答案

如果您的身份验证已经使用 SSO 服务完成,那么您应该使用 spring security 的 pre-authentication filters 之一.然后你可以指定一个 UserDetails 服务(可能是自定义的),它将使用预认证的用户原则来填充 GrantedAuthority 的

SpringSecurity 包括几个预认证过滤器,包括 J2eePreAuthenticatedProcessingFilter 和 RequestHeaderPreAuthenticatedProcessingFilter。如果您找不到适合您的应用程序,也可以自己编写,而且并不难,只要您知道您的 SSO 实现在请求中的什么位置填充数据。 (这当然取决于实现。)

只需执行 Filter接口(interface)并在 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);
}

关于authentication - spring-security:无需身份验证的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1322135/

相关文章:

asp.net - 认证子目录访问

c# - 没有指定 authenticationScheme,也没有找到 DefaultChallengeScheme - ASP.NET core 2.1

c# - IdentityServer4 与 EF6

android - 在 Android 中存储 Google AppEngine cookie

PHP 持久登录 - 我是否在每次页面身份验证后重新发出 cookie?

php - PHP 5 中的 session 处理

session - 在 Golang 中跟踪在线用户

java - AppEngine 和 session

docker - 无法在禁用匿名访问的情况下推送到Artifactory中的Docker注册表

MongoDB 未授权查询 admin.system.users