我有以下课程:
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("SampleClientId")
.secret("secret")
.authorizedGrantTypes("authorization_code")
.scopes("user_info")
.autoApprove(true) ;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
以及以下依赖项:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'org.springframework.security.oauth:spring-security-oauth2:2.3.2.RELEASE'
}
启动应用程序时,我看到以下日志:
"C:\Program Files\Java\jdk1.8.0_111\bin\java" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=50479 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.1\lib\idea_rt.jar=50480:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\work\sso\server\out\production\classes;D:\work\sso\server\out\production\resources;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\2.0.1.RELEASE\88751ed76791d12425ce5a80476baf1749a44cf4\spring-boot-starter-web-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.security.oauth\spring-security-oauth2\2.3.2.RELEASE\cf6e03591f593139f1d1d44278d962090aa226c9\spring-security-oauth2-2.3.2.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-json\2.0.1.RELEASE\f2e1aeeb1ac02bfa1b4f7254633484af1866fc65\spring-boot-starter-json-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\2.0.1.RELEASE\33abc1286b0aabea4f08ff4285d09e587835a716\spring-boot-starter-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\2.0.1.RELEASE\4b46f4aaff6c8a5a1c8184996d5e9e8a9354db8d\spring-boot-starter-tomcat-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.hibernate.validator\hibernate-validator\6.0.9.Final\b149e4cce82379f11f6129eb3187ca8ae5404005\hibernate-validator-6.0.9.Final.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\5.0.5.RELEASE\a7fd53c7ad06b0fa7dd4ff347de1b2dc508739e\spring-webmvc-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-web\5.0.4.RELEASE\bd2592c928d043f70742fd8ae409f751a63132dd\spring-security-web-5.0.4.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\5.0.5.RELEASE\d51dbb5cabe72ae02e400577bac48f7fc94088de\spring-web-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-config\5.0.4.RELEASE\355fc8c3d1c61fe85915082587946ad346250d85\spring-security-config-5.0.4.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-core\5.0.4.RELEASE\f2924cd62fa8b14546b2b3c31dcd9e55abf9a5e\spring-security-core-5.0.4.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\2.0.1.RELEASE\b0bf9d34ed70c6987a86cd58a009065e5fa02545\spring-boot-autoconfigure-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.0.1.RELEASE\b8c5b14cbb0e52fdded8f98a8c1493cc74c7cf59\spring-boot-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\5.0.5.RELEASE\9cca4bf5acb693249a01c218f471c677b951d6e2\spring-context-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\5.0.5.RELEASE\b11b61b94d7fb752a1c9bf3461d655c3084fae47\spring-aop-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\5.0.5.RELEASE\984445863c0bbdaaf860615762d998b471a6bf92\spring-beans-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\5.0.5.RELEASE\fc6c7a95aeb7d00f4c65c338b08d97767eb0dd99\spring-expression-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\5.0.5.RELEASE\1bd9feb1d9dac6accd27f5244b6c47cfcb55045c\spring-core-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\commons-codec\commons-codec\1.11\3acb4705652e16236558f0f4f2192cc33c3bd189\commons-codec-1.11.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.codehaus.jackson\jackson-mapper-asl\1.9.13\1ee2f2bed0e5dd29d1cb155a166e6f8d50bbddb7\jackson-mapper-asl-1.9.13.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\2.0.1.RELEASE\10681a28c95e9f9c0159327a1ed0c860517c7ad7\spring-boot-starter-logging-2.0.1.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api\1.3.2\934c04d3cfef185a8008e7bf34331b79730a9d43\javax.annotation-api-1.3.2.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.19\2d998d3d674b172a588e54ab619854d073f555b5\snakeyaml-1.19.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8\2.9.5\23e37f085279ba316c0df923513b81609e1d1f6\jackson-datatype-jdk8-2.9.5.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310\2.9.5\d1f0d11e816bc04e222a261106ca138801841c2d\jackson-datatype-jsr310-2.9.5.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-parameter-names\2.9.5\f824c60751ffb7bfc3a0d30dfe0e42317d8e67f5\jackson-module-parameter-names-2.9.5.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.9.5\3490508379d065fe3fcb80042b62f630f7588606\jackson-databind-2.9.5.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\8.5.29\37786f4ca8a1597a91a0f437e659a76d1fcc5bf1\tomcat-embed-websocket-8.5.29.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\8.5.29\51eac5adde4bc019261b787cb99e5548206908e6\tomcat-embed-core-8.5.29.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\8.5.29\893fb2c87ec1aa248a7911d76c0c06b3fca6bc9b\tomcat-embed-el-8.5.29.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api\2.0.1.Final\cb855558e6271b1b32e716d24cb85c7f583ce09e\validation-api-2.0.1.Final.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.jboss.logging\jboss-logging\3.3.2.Final\3789d00e859632e6c6206adc0c71625559e6e3b0\jboss-logging-3.3.2.Final.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml\classmate\1.3.4\3d5f48f10bbe4eb7bd862f10c0583be2e0053c6\classmate-1.3.4.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jcl\5.0.5.RELEASE\f4a2854b9d865e8b86717595aec16f877f8c6489\spring-jcl-5.0.5.RELEASE.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.codehaus.jackson\jackson-core-asl\1.9.13\3c304d70f42f832e0a86d45bd437f692129299a4\jackson-core-asl-1.9.13.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.2.3\7c4f3c474fb2c041d8028740440937705ebb473a\logback-classic-1.2.3.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-to-slf4j\2.10.0\f7e631ccf49cfc0aefa4a2a728da7d374c05bd3c\log4j-to-slf4j-2.10.0.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.25\af5364cd6679bfffb114f0dec8a157aaa283b76\jul-to-slf4j-1.7.25.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.9.0\7c10d545325e3a6e72e06381afe469fd40eb701\jackson-annotations-2.9.0.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.9.5\a22ac51016944b06fd9ffbc9541c6e7ce5eea117\jackson-core-2.9.5.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.2.3\864344400c3d4d92dfeb0a305dc87d953677c03c\logback-core-1.2.3.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.25\da76ca59f6a57ee3102f8f9bd9cee742973efa8a\slf4j-api-1.7.25.jar;C:\Users\ntkachev\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.10.0\fec5797a55b786184a537abd39c3fa1449d752d6\log4j-api-2.10.0.jar" com.my.sso.server.AuthorizationServerApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-12 18:07:40.163 INFO 1252 --- [ main] c.m.s.s.AuthorizationServerApplication : Starting AuthorizationServerApplication on ntkachev with PID 1252 (D:\work\sso\server\out\production\classes started by ntkachev in D:\work\sso\server)
2018-04-12 18:07:40.166 INFO 1252 --- [ main] c.m.s.s.AuthorizationServerApplication : No active profile set, falling back to default profiles: default
2018-04-12 18:07:40.204 INFO 1252 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@8e0379d: startup date [Thu Apr 12 18:07:40 MSK 2018]; root of context hierarchy
2018-04-12 18:07:41.040 INFO 1252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2018-04-12 18:07:41.060 INFO 1252 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-12 18:07:41.061 INFO 1252 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-12 18:07:41.064 INFO 1252 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_111\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\nodejs\;C:\Program Files (x86)\Skype\Phone\;C:\Windows\System32\;C:\Program Files\Java\jdk1.8.0_111\bin;C:\Program Files (x86)\apache\apache-maven-3.0.5\bin;C:\Program Files (x86)\gradle-2.3-all\gradle-2.3\bin;C:\Program Files\7-Zip;C:\Users\ntkachev\AppData\Local\Microsoft\WindowsApps;C:\Users\ntkachev\AppData\Roaming\npm;C:\Python27;D:\work\fiddler;.]
2018-04-12 18:07:41.138 INFO 1252 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-12 18:07:41.138 INFO 1252 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 936 ms
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2018-04-12 18:07:41.237 INFO 1252 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-04-12 18:07:41.268 WARN 1252 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authServerConfig': Unsatisfied dependency expressed through field 'authenticationManager'; 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: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-04-12 18:07:41.285 INFO 1252 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-04-12 18:07:41.292 INFO 1252 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-04-12 18:07:41.355 ERROR 1252 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field authenticationManager in com.my.sso.server.AuthServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.
Process finished with exit code 1
我的配置有什么问题?
实际上,我尝试从http://www.baeldung.com/sso-spring-security-oauth2重复ecxample,但没有看到此bean的明确声明
似乎我已经找到了解决问题的方法,除了我们需要添加spring-boot-starter-security依赖关系之外,从不是为您自动配置AuthenticationManager类型的spring-security 5.x bean,还需要定义一个人一个参见:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#authenticationmanager-bean
缺少的类在spring-security-core
中。尝试添加此依赖项:
compile ('org.springframework.boot:spring-boot-starter-security')
需要添加
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
@Configuration
public class UserSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationManager getAuthenticationManager() throws Exception {
return super.authenticationManagerBean();
}
}