0

我们的应用程序的身份验证是通过 siteminder 代理进行的,但授权是通过我们的应用程序控制的。

我正在使用 org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter 来检查标题。我还定义了 UserDetailsS​​ervice 来加载用户详细信息。

我还需要为用户设置角色,以便我可以使用 spring 安全标签库和其他 spring 方法来检查角色并显示选项。

我该如何实施?

我在我的用户详细信息服务实现中尝试了以下语句,但似乎不起作用。

    Authentication auth = new UsernamePasswordAuthenticationToken(user, null, roles);
    SecurityContextHolder.getContext().setAuthentication(auth);

我还阅读了有关 AbstractPreAuthenticatedProcessingFilter 类的信息,但看起来这对于此目的可能没有用。

在这个问题上的任何帮助都将非常有帮助。

谢谢!

4

1 回答 1

1

我试图在 UserDetailsS​​ervice 实现中设置角色(使用我的问题中的语句)但它不起作用。

解决方案-1:我编写了一个子类 PreAuthenticatedAuthenticationProvider 并覆盖了如下的身份验证方法:

public class CustomPreAuthenticatedAuthenticationProvider extends PreAuthenticatedAuthenticationProvider {


    @Autowired
    DBConfig dbConfig;

    @Override
    public Authentication authenticate(Authentication authentication)throws AuthenticationException {
        Authentication auth = super.authenticate(authentication);

        User user = (User)auth.getPrincipal();

        Set<Role> roles = new HashSet<Role>();

        String[] rolesArray = dbConfig.getRoles(user.getAccessLevel());
        for(String role: rolesArray){
            Role r = new Role();
            r.setName(role);
            roles.add(r);
        }

        user.setRoles(roles);

        auth = new UsernamePasswordAuthenticationToken(user, null, roles);

        return auth;
    }


}

解决方案2:我尝试在控制器中设置角色(身份验证后的主页)并且它有效。但看起来 Solution-1 是标准解决方案。

于 2015-01-08T20:09:01.907 回答