码云地址:https://gitee.com/lpxs/lp-springcloud.git 有问题可以多沟通:136358344@qq.com。
由于使用Enableoauth2sso注解一直报错,报错原因见上一篇文章《springboot2集成oauth2坑一》 后面换成了@EnableOAuth2Client注解,并自定义了相关配置
@Bean
public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
@Bean
@ConfigurationProperties("security.oauth2")
public ClientResources trina() {
return new ClientResources();
}
启动后会报下面错误:
Description:
The bean 'scopedTarget.oauth2ClientContext', defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration$SessionScopedConfiguration$ClientContextConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/oauth2/config/annotation/web/configuration/OAuth2ClientConfiguration$OAuth2ClientContextConfiguration.class] and overriding is disabled.
Action:
这个有两种方案: 1、去掉autoconfig依赖spring-security-oauth2-autoconfigure 2、使用@EnableAutoConfiguration来排除掉oauth2相关自动装配,这个得根据具体报错信息来。 如下:
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
3、增加配置spring.main.allow-bean-definition-overriding =true