el1057e:没有在上下文中注册bean解析器来解析对bean的访问

wnavrhmk  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(266)

我正在使用此资源服务器来授权请求,但每当它尝试计算表达式-“@ipwhitelistingprovider.isitvalid(request)”时,它会给出错误声明-el1057e:没有在上下文中注册bean解析器来解析对bean“ipwhitelistingprovider”的访问。

@Profile("default")
    @EnableResourceServer
    @EnableWebSecurity
    @Configuration
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
        @Autowired
        IpWhitelistingProvider ipWhitelistingProvider;

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
            .authorizeRequests()
            .antMatchers("/").permitAll()

            .antMatchers("/config/**").access("@ipWhitelistingProvider.isItValid(request) or #oauth2.hasScope('config')")
            .anyRequest().authenticated()
            .and().csrf().disable();
        }

@Component("ipWhitelistingProvider")
public class IpWhitelistingProvider{

    @Value("${ip.whitelist:1}")
    List<String>whitelist;
    String ipAcessWhitelistingString;
    public boolean isItValid(HttpServletRequest request) {

        String ip=request.getRemoteAddr();
        if(whitelist.contains(ip)) {
            return true;
        }
        else {
            return false;
        }
    }

}
llycmphe

llycmphe1#

我通过执行以下操作解决了这个问题:我的默认oauth2表达式处理程序的bean解析器为null,因此我添加了一个@bean,其中oauth2websecurityexpressionhandler显式设置应用程序上下文。

@Bean
    public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
        OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
        expressionHandler.setApplicationContext(applicationContext);
        return expressionHandler;
    }

在我的resourceserverconfigureradapter中,我配置了资源并传递了上面的bean

@Autowired
    private OAuth2WebSecurityExpressionHandler expressionHandler;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.expressionHandler(expressionHandler);
    }

相关问题