如何让Jhipster为经过身份验证的用户和匿名用户返回不同的结果

时间:2017-11-06 17:30:35

标签: spring spring-security jhipster

我使用Jhispter开发了一个搜索API,如果他们经过身份验证,则会返回用户收藏的搜索结果(例如图片)。现在我想返回相同的结果,但没有匿名用户的用户收藏夹。当我在SecurityConfiguration中将路由添加到antMatchers时,API变得不安全。虽然我仍在发送令牌,但安全上下文获取空值。我想我的问题是如何允许匿名用户,但在发送用于个性化结果的令牌时仍然具有安全上下文。

public void configure(WebSecurity web) throws Exception {
    web.ignoring()
        .antMatchers(HttpMethod.OPTIONS, "/**")
        .antMatchers("/v2/api-docs")
        .antMatchers("/swagger-ui/index.html")
        .antMatchers("/api/b2c/register")
        .antMatchers("/api/b2c/activate")
        .antMatchers("/api/b2c/reset_password/init")
        .antMatchers("/api/b2c/reset_password/finish")
        .antMatchers("/api/contact-us")
        .antMatchers("/api/search/picture")
}

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .httpBasic().realmName("server")
        .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
            .requestMatchers().antMatchers("/oauth/authorize")
        .and()
            .authorizeRequests()
            .antMatchers("/oauth/authorize").authenticated();
}

2 个答案:

答案 0 :(得分:0)

尝试从web.ignoring()中删除匹配器,并使用permitAll()访问权限将其添加到OAuth2ServerConfiguration中的匹配器

然后注册要应用于端点的OAuth2AuthenticationProcessingFilter。

@Bean
public FilterRegistrationBean customFilterRegistration(OAuth2AuthenticationProcessingFilter oAuth2AuthenticationProcessingFilter) {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(oAuth2AuthenticationProcessingFilter);
    registration.addUrlPatterns("/api/search/picture");
    registration.setName("customFilter");
    registration.setOrder(3);
    return registration;
}

答案 1 :(得分:0)

问题是OAuth2ServerConfiguration类中的antmatchers顺序。

.antMatchers(“/ api / **”)。authenticated()应该在具有permitAll()访问权限的antmatchers之后。

相关问题