前提
Spring bootでWEBアプリを作成しています。
thymeleafのth:fragmentを使って共通部分をまとめているのですが、
ログイン中でないとlayoutが適用されません。
SecurityConfigでlayoutファイルが認証時以外にも読み込まれるようにしたいのですが、
うまく設定できていない状況です。
実現したいこと
ログイン時以外でもlayoutファイルが読み込まれるようにしたい
WebSecurityConfig.java
java
1import org.springframework.context.annotation.Bean; 2import org.springframework.context.annotation.Configuration; 3import org.springframework.security.config.annotation.web.builders.HttpSecurity; 4import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 5import org.springframework.security.crypto.password.PasswordEncoder; 6import org.springframework.security.web.SecurityFilterChain; 7 8@Configuration 9public class WebSecurityConfig { 10 11 @Bean 12 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 13 14 http.formLogin(login -> login 15 .loginProcessingUrl("/login") 16 .loginPage("/login") 17 .defaultSuccessUrl("/top", true) 18 .failureUrl("/login?error") 19 .permitAll() 20 ).logout(logout -> logout 21 .logoutSuccessUrl("/login") 22 ).authorizeHttpRequests(authz -> authz 23 .antMatchers("css/**", "js/**") 24 .permitAll() 25 .mvcMatchers("/") 26 .permitAll() 27 .mvcMatchers("/register") 28 .permitAll() 29 .mvcMatchers("/validate") 30 .permitAll() 31 .anyRequest().authenticated() 32 ); 33 34 return http.build(); 35 } 36 37 @Bean 38 public PasswordEncoder passwordEncoder() { 39 return new BCryptPasswordEncoder(); 40 } 41} 42
layout.html
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 3<head> 4 <title layout:title-pattern="$CONTENT_TITLE | $LAYOUT_TITLE">DigTweet</title> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <link rel="stylesheet" th:href="@{/css/style.css}"> 8</head> 9<body> 10 <div layout:replace="~{layout/header::header}"></div> 11 <main> 12 <div layout:fragment="content"></div> 13 </main> 14 <div layout:replace="~{layout/footer::footer}"></div> 15</body> 16</html> 17
ディレクトリ構成
. ├── java │ ├── com │ │ ├── application │ │ │ ├── myapp │ │ │ │ ├── config │ │ │ │ │ └── WebSecurityConfig.java(おそらくこのファイルの設定が原因) │ │ │ │ ├── controller │ │ │ │ │ ├── AnalyticsController.java │ │ │ │ │ ├── AuthController.java │ │ │ │ │ ├── IndexController.java │ │ │ │ │ └── MemoCRUDController.java │ │ │ │ ├── entity │ │ │ │ │ ├── Memo.java │ │ │ │ │ ├── TmpUser.java │ │ │ │ │ └── User.java │ │ │ │ ├── form │ │ │ │ │ └── UserForm.java │ │ │ │ ├── repository │ │ │ │ │ ├── MemoRepository.java │ │ │ │ │ ├── TmpUserRepository.java │ │ │ │ │ └── UserRepository.java │ │ │ │ ├── service │ │ │ │ │ ├── AnalyticsService.java │ │ │ │ │ ├── MemoCRUDService.java │ │ │ │ │ └── UserService.java │ │ │ │ ├── utility │ │ │ │ │ ├── .DS_Store │ │ │ │ │ ├── CreateMail.java │ │ │ │ │ ├── FrequencyList.java │ │ │ │ │ ├── LDTFormatter.java │ │ │ │ │ ├── RandomMessage.java │ │ │ │ │ ├── SentiScoreMap.java │ │ │ │ │ ├── TargetSplit.java │ │ │ │ │ └── UpdateUserContext.java │ │ │ │ ├── .DS_Store │ │ │ │ └── Application.java │ │ │ └── .DS_Store │ │ └── .DS_Store │ └── .DS_Store ├── resources │ ├── files │ │ ├── .DS_Store │ │ ├── LEngineer-Regular.otf │ │ └── pn.csv.m3.120408.trim │ ├── static │ │ ├── css │ │ │ └── style.css │ │ └── js │ │ └── dialog.js │ ├── templates │ │ ├── analytics │ │ │ └── analytics.html │ │ ├── auth │ │ │ ├── login.html │ │ │ ├── register_result.html │ │ │ └── register.html │ │ ├── contact │ │ │ └── contact.html │ │ ├── crud │ │ │ ├── archive.html │ │ │ ├── create.html │ │ │ ├── edit.html │ │ │ └── search.html │ │ ├── home │ │ │ └── home.html │ │ ├── layout(このフォルダ直下の3つのファイルを読み込みたい) │ │ │ ├── footer.html │ │ │ ├── header.html │ │ │ └── layout.html │ │ ├── mypage │ │ │ └── mypage.html │ │ ├── top │ │ │ └── top.html │ │ └── .DS_Store │ ├── .DS_Store │ └── application.yml └── .DS_Store.
試したこと
的外れかもですが、、、
・antMatcher内に「"layout/"」「"../templates/layout/"」「"../templates/layout/layout"」などと記載を試みる
・layoutフォルダをtemplatesからstaticに移動
などを試しましたが、解決せずでした。
補足情報(FW/ツールのバージョンなど)
openjdk version "17.0.3"
springframework.boot version '2.7.0'
調べたところ、thymeleafのlayoutファイル自体は読み込まれているようでした。
ただ、cssやjsといった静的ファイルが、おそらくconfigの設定が原因で読み込まれていないようです。
https://www.docswell.com/s/MasatoshiTada/KGVY9K-spring-security-intro#p19
上記スライドを参考に、WebSecurityConfigの
.antMatchers("css/**", "js/**")
.permitAll()
の部分を下記のように書き換えました。
.requestMatchers(PathRequest.toStaticResources().atCommonLocations())
.permitAll()
がしかし、下記のエラーが出ています。
The method requestMatchers(RequestMatcher...) in the type AbstractRequestMatcherRegistry<AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizedUrl> is not applicable for the arguments (StaticResourceRequest.StaticResourceServerWebExchange)Java(67108979)
引数の指定の仕方などが違うのかと思ったのですが、何を入れていいやらさっぱりわからず、、、
どなたかご助言いただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー