Thymeleafセキュリティ方言(sec:authorizeタグなど)を、正常に機能しているSpring Boot + Spring Securityアプリケーションに統合しようとしています。
いくつかの調査の後、私はそれを活性化する解決策が以下であることを発見しました:
POMファイルに依存関係を追加します。
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
テンプレートファイルの先頭にタグを含めます。
<html xmlns:th="http://www.thymeleaf.org" lang="en"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
ここまでは順調ですね。依存関係が見つかり、タグがマークアップで認識されます。
ただし、それらは考慮されず、生成された最終的なHTMLに表示されます。
有効にしないSpring Boot自動設定の問題のため、手動でSpringSecurityDialect Beanを1つの@Configurationクラスに追加して有効にする必要があるようです(StackOverflowで見つかったいくつかの質問にはこれによって解決されました):
@Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}
これが問題の原因です。このBeanをSpringブート構成に追加すると、クラスorg.thymeleaf.dialect.IProcessorDialectが見つからないため、例外が発生します。ここにエラーがあります:
> Java.lang.IllegalStateException: Could not evaluate condition on
> org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
> due to org/thymeleaf/dialect/IProcessorDialect not found. Make sure
> your own configuration does not rely on that class. This can also
> happen if you are @ComponentScanning a springframework package (e.g.
> if you put a @ComponentScan in the default package by mistake)
何が欠けていますか?これはThymeleafからのエラーですか?Spring Boot?自分のコード?助けてくれてありがとう!
これは私の問題に関係する私のファイルの一部です:
Application.Java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory Tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
Tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return Tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.Apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
}
WebMvcConfig.Java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/**
* Configure relationships between URLs and view names
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
}
@Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}
}
Thymeleafテンプレート:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
....
<div sec:authorize="isAuthenticated()">
LOGGED IN
</div>
<div sec:authorize="isAnonymous()">
ANONYMOUS
</div>
....
アプリケーション起動時の完全なコンソール出力:
> :: Spring Boot :: (v1.3.4.RELEASE)
>
> 2016-05-17 17:22:59.951 INFO 96267 --- [ restartedMain]
> edu.rmit.eres.estored.Application : Starting Application on
> w8031808.local with PID 96267
> (/Users/guillaume/dev/workspace/e-stored/target/classes started by
> guillaume in /Users/guillaume/dev/workspace/e-stored) 2016-05-17
> 17:22:59.956 INFO 96267 --- [ restartedMain]
> edu.rmit.eres.estored.Application : No active profile set,
> falling back to default profiles: default 2016-05-17 17:23:00.239
> INFO 96267 --- [ restartedMain]
> ationConfigEmbeddedWebApplicationContext : Refreshing
> org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16f53cde:
> startup date [Tue May 17 17:23:00 AEST 2016]; root of context
> hierarchy 2016-05-17 17:23:01.578 ERROR 96267 --- [ restartedMain]
> o.s.boot.SpringApplication : Application startup failed
>
> Java.lang.IllegalStateException: Could not evaluate condition on
> org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
> due to org/thymeleaf/dialect/IProcessorDialect not found. Make sure
> your own configuration does not rely on that class. This can also
> happen if you are @ComponentScanning a springframework package (e.g.
> if you put a @ComponentScan in the default package by mistake) at
> org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.Java:55)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.Java:102)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.Java:178)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.Java:140)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.Java:116)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.Java:333)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.Java:243)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.Java:273)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.Java:98)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.Java:678)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:520)
> ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.Java:118)
> ~[spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:766)
> [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.Java:361)
> [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.Java:307)
> [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.Java:1191)
> [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.SpringApplication.run(SpringApplication.Java:1180)
> [spring-boot-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> edu.rmit.eres.estored.Application.main(Application.Java:24)
> [classes/:na] at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:1.8.0_60] at
> Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62)
> ~[na:1.8.0_60] at
> Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
> ~[na:1.8.0_60] at Java.lang.reflect.Method.invoke(Method.Java:497)
> ~[na:1.8.0_60] at
> org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.Java:49)
> [spring-boot-devtools-1.3.4.RELEASE.jar:1.3.4.RELEASE] Caused by:
> Java.lang.NoClassDefFoundError:
> org/thymeleaf/dialect/IProcessorDialect at
> Java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_60] at
> Java.lang.ClassLoader.defineClass(ClassLoader.Java:760) ~[na:1.8.0_60]
> at
> Java.security.SecureClassLoader.defineClass(SecureClassLoader.Java:142)
> ~[na:1.8.0_60] at
> Java.net.URLClassLoader.defineClass(URLClassLoader.Java:467)
> ~[na:1.8.0_60] at
> Java.net.URLClassLoader.access$100(URLClassLoader.Java:73)
> ~[na:1.8.0_60] at
> Java.net.URLClassLoader$1.run(URLClassLoader.Java:368) ~[na:1.8.0_60]
> at Java.net.URLClassLoader$1.run(URLClassLoader.Java:362)
> ~[na:1.8.0_60] at Java.security.AccessController.doPrivileged(Native
> Method) ~[na:1.8.0_60] at
> Java.net.URLClassLoader.findClass(URLClassLoader.Java:361)
> ~[na:1.8.0_60] at
> Java.lang.ClassLoader.loadClass(ClassLoader.Java:424) ~[na:1.8.0_60]
> at Sun.misc.Launcher$AppClassLoader.loadClass(Launcher.Java:331)
> ~[na:1.8.0_60] at
> Java.lang.ClassLoader.loadClass(ClassLoader.Java:357) ~[na:1.8.0_60]
> at
> org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.Java:151)
> ~[spring-boot-devtools-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> Java.lang.ClassLoader.loadClass(ClassLoader.Java:357) ~[na:1.8.0_60]
> at Java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_60]
> at Java.lang.Class.privateGetDeclaredMethods(Class.Java:2701)
> ~[na:1.8.0_60] at Java.lang.Class.getDeclaredMethods(Class.Java:1975)
> ~[na:1.8.0_60] at
> org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.Java:612)
> ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.Java:524)
> ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.Java:510)
> ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.Java:570)
> ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.Java:683)
> ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.Java:627)
> ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.Java:597)
> ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.Java:1445)
> ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.Java:975)
> ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
> org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanTypeForNonAliasDefinition(BeanTypeRegistry.Java:289)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanType(BeanTypeRegistry.Java:278)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.getNamesForType(BeanTypeRegistry.Java:259)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.Java:182)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.Java:171)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.Java:139)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.Java:113)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] at
> org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.Java:47)
> ~[spring-boot-autoconfigure-1.3.4.RELEASE.jar:1.3.4.RELEASE] ... 22
> common frames omitted Caused by: Java.lang.ClassNotFoundException:
> org.thymeleaf.dialect.IProcessorDialect at
> Java.net.URLClassLoader.findClass(URLClassLoader.Java:381)
> ~[na:1.8.0_60] at
> Java.lang.ClassLoader.loadClass(ClassLoader.Java:424) ~[na:1.8.0_60]
> at Sun.misc.Launcher$AppClassLoader.loadClass(Launcher.Java:331)
> ~[na:1.8.0_60] at
> Java.lang.ClassLoader.loadClass(ClassLoader.Java:357) ~[na:1.8.0_60]
> ... 56 common frames omitted
>
> 2016-05-17 17:23:01.581 INFO 96267 --- [ restartedMain]
> .b.l.ClasspathLoggingApplicationListener : Application failed to start
> with classpath:
> [file:/Users/guillaume/dev/workspace/e-stored/target/classes/]
M. Deinumの役立つコメントをありがとう!
実際、「Thymeleaf Extras for Spring Security 4」バージョン3.0.0はまだサポートされていないようです。
私のPOMファイルのThymeleafのMaven依存バージョンthymeleaf-extras-springsecurity4を3.0.0から最新の2.xxバージョン(この投稿の時点では2.1.2)に変更しました。問題を修正しました。
から:
<!-- http://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
に:
<!-- http://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
問題は発生しなくなり、Webアプリケーションが正常に起動し、タグが認識されます。
これは私の命を救います。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
使用:Maven 3.5.0
Java 1.8.0_152
春: https://start.spring.io ->春のブーツ::(v2.0.0.BUILD-SNAPSHOT)
最後に、デフォルトのスプリングブート構成で次のように動作します-スプリングブートバージョン1.5.8.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
これは追加されます
Spring-security 4.2.3.RELEASE
設定ファイルのBean定義SpringSecurityDialectは無視してください。機能しません。他のことをする必要がない限り、デフォルトを使用してください。