今天学习如何配置spring cloud 安全认证,发现一个错误,错误的信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Can't configure anyRequest after itself
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:645) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:475) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at com.cloud.discovery.CloudDiscoveryApplication.main(CloudDiscoveryApplication.java:15) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Can't configure anyRequest after itself
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.IllegalStateException: Can't configure anyRequest after itself
    at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.anyRequest(AbstractRequestMatcherRegistry.java:74) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.configure(WebSecurityConfigurerAdapter.java:355) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at com.cloud.discovery.CloudDiscoveryApplication$WebSecurityConfigure.configure(CloudDiscoveryApplication.java:32) ~[classes/:na]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:231) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:322) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:92) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at com.cloud.discovery.CloudDiscoveryApplication$WebSecurityConfigure$$EnhancerBySpringCGLIB$$34483344.init(<generated>) ~[classes/:na]
    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:370) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:324) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:41) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:104) ~[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 22 common frames omitted

关键信息是:Can't configure anyRequest after itself

产生原因,我引用的代码为:

@EnableWebSecurity
    static class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 在/eureka/**端点忽略csrf验证
            http.csrf().ignoringAntMatchers("/eureka/**");
            // 配置使请求需要通过httpBasic或form验证
            http.authorizeRequests().anyRequest()
                    .authenticated()
                    .and()
                    .formLogin()
                    .and()
                    .httpBasic();
            super.configure(http);
        }
    }

这是一个网上spring cloud的代码示例,示例代码使用的spring cloud版本是 2.0.1,而我在环境中使用spring 版本是2.2.1(Hoxton.RELEASE)。

原因是:anyRequest 多次被调用,所以将多余的anyRequest去掉即可。

---- 我检查了下代码,没有发现有anyRequest被多次调用,是不是代码内部的多次调用呢?所幸注销掉,代码可以顺利跑起来。

今天学习spring cloud,在启动运行Eureka server的时候发生如下错误:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.cloud.discovery.CloudDiscoveryApplication$WebSecurityConfigure]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:597) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:305) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processMemberClasses(ConfigurationClassParser.java:364) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:266) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:325) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:242) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at com.cloud.discovery.CloudDiscoveryApplication.main(CloudDiscoveryApplication.java:15) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:165) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:395) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:320) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:587) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 19 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:477) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:451) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:154) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 23 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/Filter
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_92]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_92]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_92]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:459) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 25 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_92]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_92]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_92]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_92]
    ... 29 common frames omitted

我们提取关键信息,发现错误为:

Failed to introspect annotated methods on class org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration

无法编译使用注解标签:WebSecurity

网上寻找了一番发现,我在使用tomcat支持的时候引用的范围太小,没有涵盖标签编译,所以导致无法使用。正确的使用方法是:

<!-- tomcat支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

修改作用域scope:provided改为scope = compile,即为:

<!-- tomcat支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>

再运行没有发生类似的错误,成功解决!

1) 首先是数据的打通,数据在多个设备间同步是所有的基础。

你的多个iPhone/iPad和MacBook上的很多资料就被iCloud无感同步了,比如:照片、文档、备忘录、通讯录、提醒、日历、WiFi密码、Safri网页密码…… 各种数据的无感打通让你感觉再不用找数据线了传数据了……如果有多个iPhone手机,你就会发现,你的电话通话记录在多个设备间也是实时同步的……

数据的分享还可以在亲朋好友间,如果是通讯录的联系人,当你的朋友在你身边要连接一个需要密码的WiFi时,你的手机就会出现共享密码,不必担心安全问题,因为密码是以一个加密串共享给对方的……

2)然后是功能和操作上打通,你的多个设备的功能开始打通。

  • 你的所有设备间的网络连接(个人热点)也是无缝共享的。
  • 最新的Catalina和iPad的双屏随航模式就很有想像力的,一个手写板就出来了
  • 超级剪贴版,一个地方复制,所有的设备都可以粘贴,除了文字外,图片一样可以在各个设备间复制粘贴(我在手机上看到一个图片,复制一下,到电脑上打开Word直接粘贴上了)
  • 手机看邮件和Safri访问网页的时候,电脑的Dock条上就会出现一个同步的图标,一点了以后就可以把工作传递到电脑上来。
  • 在Word文档的右键菜单中有一项“从iPhone中插入”,你的所有的iPhone都会在列表里,你可以点拍照和扫描文稿,于是就会在无线的情况下开启你的手机摄影模式,拍完照后就上传到电脑上了。
  • 在电脑上写的文档,在手机接着写,在手机上保存的东西,在电脑上整理,一切都是无感同步……
  • 电话来的时候,如果你的手机在充电,你可以在电脑上接电话(对我来说,就是两个手机同时响,如果不接,电脑开始响……)

临近双十一各电商平台推出自己的分享计划,其中京东联盟也推出了自己的特色服务。不看不知道,一看吓一跳,现在的京东联盟已经发展得很完善,各类推广应有尽有。之前京东联盟的申请门槛很高,所以申请一直作罢,这几天听到群里的小伙伴说基本上备案的网站都能申请通过,我也抱着试一试的心态去申请。

不过申请之路并不顺利,中间遇到了一个小问题,在这里分享一下。这个问题其实不难,也就是在添加网站进行校验的时候,发生“无法访问”的错误。

WX20191029-235344@2x.png

笔者的网站是使用https的,在之前有遇到过跳转的问题无法验证,但是我使用浏览器打开jd_root.txt文件是可以直接访问的,在使用Curl工具也显示没有任何跳转,信息如下:

curl -i https://xxx.com/jd_root.txt
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 29 Oct 2019 15:19:13 GMT
Content-Type: text/plain
Content-Length: 48
Last-Modified: Tue, 29 Oct 2019 15:14:56 GMT
Connection: keep-alive
ETag: "5db85770-30"
Accept-Ranges: bytes

e95d2f4a675fe6f2c395df6aba224bf0a0bab669227c60a7
 

注意response的状态=200,说明使用https并没有跳转且能够访问得到该Jd_root.txt串。那么怎么办呢?

我试着去修改nginx的配置,关闭433端口,启用80端口,并且关闭301跳转。重启nginx以后重试,果然校验过去!

分析

由于笔者以前是Jd的员工,所以知道京东后台使用的Java语言编写,而Java在访问https的时候确实不那么容易实现。所以笔者猜测后台人员为了图省事,所以没有实现Java的https访问机制!

也就是必须使用80端口来校验才OK!

今天使用新安装wam7.com网站,安装过程中都无事。安装完以后想去初始化,就出现空白页面,如图所示:

空白页面

即提示错误:

无法访问此网站 找不到 www.wam7.com 的服务器 IP 地址。
DNS_PROBE_FINISHED_NXDOMAIN

百度和google了都没有答案,ping也能ping不同,但是明明设置了dns解析。后来不经意之间发现了一个提示:

WX20190930-001042@2x.png

显示当前的DNS为:

ns3.dns.com    
ns4.dns.com    

提示修改为:

dns11.hichina.com
dns12.hichina.com

看来域名的DNS没有设置过来,导致在阿里云设置的域名解析并没有生效!按照提示,点击域名管理-更改DNS选项,进入页面:

WX20190930-001024@2x.png

在此页面更改后,访问一切正常!