且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

春天的安全性:授权无需验证

更新时间:2023-12-04 19:34:46

如果您的身份验证使用SSO服务已经完成,那么你应该使用Spring Security的有一个$p$p-authentication过滤器。然后,你可以指定一个UserDetails服务(可能为自定义),将使用pre认证的用户原则来填充的GrantedAuthority的

If your authentication is already done using an SSO service, then you should use one of spring security's pre-authentication filters. Then you can specify a UserDetails service (possibly custom) that will use the pre-authenticated user principle to populate the GrantedAuthority's

SpringSecurity包括几个pre-认证过滤器包括J2EE preAuthenticatedProcessingFilter和RequestHeader preAuthenticatedProcessingFilter。如果你不能找到一个适合你,它也有可能,而且并不难写自己的,只要你知道在请求您的SSO实现充塞的数据。 (这取决于课程的实施。)

SpringSecurity includes several pre-authentication filters including J2eePreAuthenticatedProcessingFilter and RequestHeaderPreAuthenticatedProcessingFilter. If you can't find one that works for you, its also possible, and not that hard to write your own, provided you know where in the request your SSO implementation stuffs the data. (That depends on the implementation of course.)

只需实现过滤器接口做这样的事情在doFilter方法​​:

Just implement the Filter interface and do something like this in the doFilter method:

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);
}