gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:11:32 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com