【问题标题】:Using @PreAuthorize or @Secured with Jersey when using Configuration Class使用配置类时使用 @PreAuthorize 或 @Secured 和 Jersey
【发布时间】:2017-01-10 02:34:54
【问题描述】:

我遇到了类似于PreAuthorize annotation doesn't work with jersey 的问题。我为 Spring Security 创建了一个配置类,并且身份验证有效,但授权无效。

这是我的代码

SpringSecurityConfig.java

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Order(1)
@ComponentScan({"com.foo.rest.resources.Template"})
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserService userService;
    private final TokenAuthenticationService tokenAuthenticationService;

    public SpringSecurityConfig() {
        super(true);
        this.userService = new UserService();
        tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http
                .exceptionHandling().and()
                .anonymous().and()
                .servletApi().and()
                .headers().cacheControl().and()
                .authorizeRequests()
                // Allow anonymous logins
                .antMatchers("/auth/**").permitAll()
                // All other request need to be authenticated
                .anyRequest().authenticated().and()

                // Custom Token based authentication based on the header previously given to the client
                .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService),
                        UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    @Override
    public UserService userDetailsService() {
        return userService;
    }

    @Bean
    public TokenAuthenticationService tokenAuthenticationService() {
        return tokenAuthenticationService;
    }
}

Template.java

@Component
@Path("/template")
@Produces(MediaType.APPLICATION_JSON)
public class Template {

    @GET
    @Secured("ROLE_EDITOR")
    public User getTemplate() {
        return new Template();
    }
}

我的猜测是身份验证是在过滤器链中处理的,但在达到授权标签后它就再也不会回来了。知道如何进行这项工作吗?

【问题讨论】:

  • 访问其余服务的用户是否在您的用户服务中配置了“ROLE_EDITOR”角色??
  • 这只是原始代码中的一个错字,已修复。还是同样的问题。

标签: java spring rest spring-security jersey


【解决方案1】:

我认为您的@ComponentScan 配置错误并且没有正确选择Template 资源。

根据@ComponentScan documentation,该值是basePackages 的别名,但您给出的是类而不是包。尝试将其更改为如下所示。

@ComponentScan({"com.foo.rest.resources.*"})

根据documentation,确保您没有错过 Jersey Spring Integration 中的任何步骤

【讨论】:

  • 我需要做很多事情,但文档和您的回答让我完成了大约 90% 的工作。
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 2014-12-07
  • 2017-10-13
  • 2016-07-18
  • 2013-05-01
  • 1970-01-01
  • 2020-09-24
  • 2011-09-11
相关资源
最近更新 更多