Java Spring安全性无法识别用户角色

Java Spring安全性无法识别用户角色,java,angularjs,spring,spring-mvc,spring-security,Java,Angularjs,Spring,Spring Mvc,Spring Security,它的行为看起来很奇怪,可能是一只虫子。 我有一个SpringBoot1.3.2作为使用Rest服务的后端API,还有一个使用Angular 2的应用程序使用这些服务。 所有的安全措施都可以使用JWT令牌,我可以为登录用户限制我的服务,我可以检查登录的用户,等等。如果我使用某个用户角色添加我的服务@Secured或@PreAuthorize,则授权无法100%正常工作。使用@WithMockUser(roles=“role\u TEST”)可以使用swagger和我的MockMvc测试,因此它的配

它的行为看起来很奇怪,可能是一只虫子。 我有一个SpringBoot1.3.2作为使用Rest服务的后端API,还有一个使用Angular 2的应用程序使用这些服务。 所有的安全措施都可以使用JWT令牌,我可以为登录用户限制我的服务,我可以检查登录的用户,等等。如果我使用某个用户角色添加我的服务
@Secured
@PreAuthorize
,则授权无法100%正常工作。使用
@WithMockUser(roles=“role\u TEST”)
可以使用swagger和我的MockMvc测试,因此它的配置正常

问题是,当我通过angular应用程序访问时,使用
@Secured
@PreAuthorize
的授权不起作用,所有已
@Secured
@PreAuthorize
的请求都会收到状态403

authentication.getAuthories()
我的所有角色都被完美加载

控制器:

@RequestMapping(method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@Secured("ROLE_MANTER_INSTITUICAO")
public List<HierarquiaInstituicao> getAll() {
    return service.findAll();
}
我的过滤器:

public class StatelessAuthenticationFilter extends GenericFilterBean {

    private final TokenAuthenticationService authenticationService;

    public StatelessAuthenticationFilter(TokenAuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        Authentication authentication = authenticationService.getAuthentication(httpRequest);
        if(authentication != null) {
            SecurityContextHolder.getContext().setAuthentication(authentication);
            filterChain.doFilter(request, response);
        }
        else {
            httpResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
        }
        SecurityContextHolder.getContext().setAuthentication(null);

    }
}

我们可以有一个简单的代码示例吗?当身份验证加载到无状态AuthenticationFilter中时,它是否加载了角色?@mad_fox Yes如果我加载了Collection Authories=(Collection)SecurityContextHolder.getContext().getAuthentication().GetAuthories();我可以看到加载的所有角色是加载的角色“role_MANTER_INSTITUICAO”还是加载的角色“MANTER_INSTITUICAO”。如果是前者,请尝试将其更改为MANTER_INSTITUICAO。在数据库中记录为“ROLE_MANTER_INSTITUICAO”。我尝试使用ROLE_uu前缀,但没有ROLE_uu,我的应用程序因此错误而停止。我在spring工作了10年,但这次我不知道该怎么办。我们能有一个简单的代码示例吗?当身份验证加载到无状态AuthenticationFilter中时,它是否加载了角色?@mad_fox Yes如果我加载了Collection Authority=(Collection)SecurityContextHolder.getContext().getAuthentication().GetAuthorients();我可以看到加载的所有角色是加载的角色“role_MANTER_INSTITUICAO”还是加载的角色“MANTER_INSTITUICAO”。如果是前者,请尝试将其更改为MANTER_INSTITUICAO。在数据库中记录为“ROLE_MANTER_INSTITUICAO”。我尝试使用ROLE_uu前缀,但没有ROLE_uu,我的应用程序因此错误而停止。我在spring工作了10年,但这次我不知道该怎么办。
public class StatelessAuthenticationFilter extends GenericFilterBean {

    private final TokenAuthenticationService authenticationService;

    public StatelessAuthenticationFilter(TokenAuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        Authentication authentication = authenticationService.getAuthentication(httpRequest);
        if(authentication != null) {
            SecurityContextHolder.getContext().setAuthentication(authentication);
            filterChain.doFilter(request, response);
        }
        else {
            httpResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
        }
        SecurityContextHolder.getContext().setAuthentication(null);

    }
}