急求,SpringSecurity项目,本地打包测试没有任何问题,但是部署到Linus中的Docker报AuthenticationManage注入报错

u011013470 2019-07-04 05:53:03

SpringBoot2 + SpringSecurity5 + SpringCloud2项目,本地打包测试没有任何问题,但是部署到Linus中的Docker报AuthenticationManage注入的报错


公共权限模块当中的主配制基类

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

/**
* AuthenticationManager 这个Bean 已经在这里定义好了,在本地开发启动不报错,在测试环境Docker环境和JAR启动都报同样的错误
**/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}


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

}

授权模块当中的主配制类

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AuthSecurityConfig extends WebSecurityConfig {

@Resource
private SecurityUserDetailsService securityUserDetailsService;

@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false);
provider.setUserDetailsService(this.securityUserDetailsService);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}

@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
// 增加
.authenticationProvider(authenticationProvider());
}

@Bean
public UserDetailsService securityUserDetailsService() {
return new SecurityUserDetailsService();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(securityUserDetailsService());
}


}

公共权限模块当中的OAuth2主配制类:

@Configuration
@EnableResourceServer
public class WebResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Resource
private AuthenticationSuccessHandler loginAuthenticationSuccessHandler;
@Resource
private AuthenticationFailureHandler webAuthenticationFailureHandler;
@Resource
private LogoutSuccessHandler webLogoutSuccessHandler;
@Resource
private WebAccessHandler webAccessHandler;
@Resource
private AuthorizeConfigManager authorizeConfigManager;
@Resource
private OAuth2WebSecurityExpressionHandler expressionHandler;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.expressionHandler(expressionHandler);
}

@Override
public void configure(HttpSecurity http) throws Exception {
http
// 表单登录,
.formLogin()
.loginPage(WebUrlProperties.LOGIN_URL) // 定义当前用户登陆的时候,跳转的登录页面
.loginProcessingUrl(WebUrlProperties.LOGIN_FROM_URL)
// 配置登陆成功处理器
.successHandler(loginAuthenticationSuccessHandler)
// 配置登陆失败处理器
.failureHandler(webAuthenticationFailureHandler)
.permitAll()
.and()
// 用户退出
.logout()
.logoutSuccessHandler(webLogoutSuccessHandler)
.and()
// 不过滤的URL
.authorizeRequests()
.antMatchers(
WebUrlProperties.REGISTER_URL, // 注册接口
.permitAll()
.and()
// session管理
.sessionManagement()
// 当session过期失效的时候,要跳转的地址
.invalidSessionUrl(WebUrlProperties.SESSION_URL) // Session会话失效URL
// 并发登录用户的总数
.maximumSessions(100)
// 当Session达到上限的时候,阻止用户登录
.maxSessionsPreventsLogin(true)
.expiredSessionStrategy(new WebSessionInformationExpiredStrategy());

http.addFilterBefore(customAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class);
http.exceptionHandling().accessDeniedHandler(webAccessHandler);
http.csrf().disable();
authorizeConfigManager.config(http.authorizeRequests());
}

}

公共权限模块当中的OAuth2的Token配置类

@Configuration
@EnableAuthorizationServer
public class WebAuthorizationSeverConfig extends AuthorizationServerConfigurerAdapter {
@Resource
private AuthenticationManager authenticationManager;
@Resource
private UserDetailsService userDetailsService;
@Resource
private SecurityProperties securityProperties;
@Resource
private TokenStore tokenStore;
@Resource
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Resource
private TokenEnhancer jwtTokenEnhancer;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
// 引入TokenStore token存储配置类,将令牌存入Redis当中
.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// 不需要第三方平台进行注册,这块只是给客户端提供服务,所以用inMemory()内存方法就可以了
clients.inMemory()
.withClient("sup" /*config.getClientId()*/)
// 指定client密码
.secret("supsecret" /*config.getClientSecret()*/)
.redirectUris("http://example.com")
// 发出的令牌的有效时间是多少,如果是默认0的话,就说明这个令牌是不会过期的
.accessTokenValiditySeconds(7200 /*config.getAccessTokenValiditySeconds()*/)
// 对当前的应用所支持的授权模式有哪些,password指的是当前应用的授权模式和授权类型
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
// 刷新Token,再设置一次Token的时间
.refreshTokenValiditySeconds(36000)
// 你能够发出去的权限有哪些,加了这些参数之后,测试所发送的参数就是这些其中之一
.scopes("all", "read", "write");
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.tokenKeyAccess("permitAll()")
.checkTokenAccess("permitAll()")
// 指定不对clientSecret进行加密
.passwordEncoder(NoOpPasswordEncoder.getInstance())// ;
.allowFormAuthenticationForClients();
}
}

其他的继承UserDetailsService的类就不再这里一一展示了。

但是我本地再IDEA当中继承开发不报错,已部署到环境上面就报这个错,都整了好几天了,也没有结果!

在测试环境Docker环境和JAR启动都报同样的错误,就是以下这个错误:

ERROR [-,,,] 32479 --- [ main] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'webResourceServerConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAuthenticationSuccessHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration': Unsatisfied dependency expressed through field 'configurers'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webAuthorizationSeverConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
2019-07-04 17:33:26.162[,] [main] ERROR o.s.boot.web.embedded.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'webResourceServerConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginAuthenticationSuccessHandler': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration': Unsatisfied dependency expressed through field 'configurers'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webAuthorizationSeverConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
2019-07-04 17:33:26.345 INFO [-,,,] 32479 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-07-04 17:33:26.345[,] [main] INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat]




有没有技术大牛,或者是好心的大哥大姐遇到过这个问题呢,帮忙给看一下,小弟在这里感谢各位了!!!



...全文
168 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011013470 2019-07-06
引用 4 楼 人到中年就秃头 的回复:
你看看docker中的java jre版本是否一致,springboot线上运行方式使用springboot内置tomcat运行,就是使用shell命令运行jar包,java -jar 包名
都是一致的,这个JAR我这我本地和CentOS服务器上,用 java -jar app.jar 也是报同样的错误。 现在,改了版本之后,还是报其他错误了,这几天真是把我整得焦头烂额额啊!!!!
回复
你看看docker中的java jre版本是否一致,springboot线上运行方式使用springboot内置tomcat运行,就是使用shell命令运行jar包,java -jar 包名
回复
faith.huan 2019-07-05
WebSecurityConfig add @Configuration, kankan xing bu,shu ru fa bu hao shi
回复
u011013470 2019-07-05
到底有没有人知道,这个问题如何解决???
回复
u011013470 2019-07-04
测试环境日志最后面报的错还是: *************************** APPLICATION FAILED TO START *************************** Description: Field configurers in org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration. 2019-07-04 18:13:15.796[,] [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: Field configurers in org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告