serverhttpsecurity错误

3zwtqj6y  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(426)

我正在尝试将Spring Security 添加到我的spring启动应用程序中。我创建了新的securityconfig类,并添加了以下代码:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain configSecurity(ServerHttpSecurity http) {
        return http
            .authorizeExchange(exchange -> exchange.matchers(EndpointRequest.toAnyEndpoint()).permitAll().anyExchange().authenticated())
            .oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::jwt)
            .build();
    }

}
当我尝试启动应用程序时,出现错误:
2021-04-08 08:58:47.828 info 3040---[main]o.s.s.web.defaultsecurityfilterchain:将使用[org.springframework.security.web.context.request.async]保护任何请求。webasyncmanagerintegrationfilter@6089c1e3,org.springframework.security.web.context。securitycontextpersistencefilter@4bba628,org.springframework.security.web.header。headerwriterfilter@47b4fcd5,org.springframework.security.web.csrf。csrffilter@439acac0,org.springframework.security.web.authentication.logout。logoutfilter@6af447b6,org.springframework.security.oauth2.server.resource.web。bearertokenauthenticationfilter@5679e464,org.springframework.security.web.savedrequest。requestcacheawarefilter@4420f4d4,org.springframework.security.web.servletapi。securitycontextholderawarerequestfilter@517594dd,org.springframework.security.web.authentication。anonymousauthenticationfilter@67231d8d,org.springframework.security.web.session。sessionmanagementfilter@59b5251d,org.springframework.security.web.access。exceptiontranslationfilter@7d6b0636,org.springframework.security.web.access.intercept。filtersecurityinterceptor@4b50ebea]
2021-04-08 08:58:47.883 warn 3040---[main]configservletwebserverapplicationcontext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatifiedDependencyException:使用类路径资源[it/config/securityconfig.class]中定义的名称“configsecurity”创建bean时出错:通过方法“configsecurity”参数0表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有类型为“org.springframework.security.config.web.server.serverhttpsecurity”的合格bean可用:至少需要1个符合autowire候选的bean。依赖项注解:{}
以及:
应用程序启动失败
描述:it.config.securityconfig中configsecurity方法的参数0需要类型为“org.springframework.security.config.web.server.serverhttpsecurity”的bean,但找不到该bean。
操作:考虑在配置中定义类型为“org.springframework.security.config.web.server.serverhttpsecurity”的bean。
你知道怎么解决吗?
在我的依赖之下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
anauzrmj

anauzrmj1#

尝试用以下内容注解bean所在的类:

@Configuration
@EnableWebSecurity

另外,确保bean是公共的-看看是否所有这些都修复了它?如果没有-您能否提供异常的完整堆栈跟踪。
谢谢,本

gudnpqoy

gudnpqoy2#

尝试在顶部添加所有这些注解。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)

有时 @EnableGlobalMethodSecurity 帮助

polhcujo

polhcujo3#

我已经解决了使用“httpsecurity”而不是“httpsecurity”的问题。下面是我的新代码:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest().authenticated()).oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
   }
}

相关问题