Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( Spring Session ã使ç¨ãã )
æ¦è¦
Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( ãã°ã¤ã³ç»é¢ã®ä½æï¼ ) ã®ç¶ãã§ãã
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ã»ãã·ã§ã³ã®ç®¡çã« Spring Session ã使ç¨ãã
åç §ãããµã¤ãã»æ¸ç±
-
Spring Session
http://projects.spring.io/spring-session/- Spring Session ã®å ¬å¼ãµã¤ãã§ãã
-
Spring Bootãã³ãºãªã³ - 5. Spring Sessionã®å°å ¥
http://jsug-spring-boot-handson.readthedocs.org/en/latest/SpringSession.html- Spring Boot 㧠Spring Session ã使ç¨ããããã®è¨å®æ¹æ³ãåç §ãã¾ããã
ç®æ¬¡
- ã¯ããã«
- 1.0.x-use-spring-session ãã©ã³ãã®ä½æ
- Spring Session ã使ç¨ãã¦ããªãã¨ã©ããªãã®ãï¼
- Spring Session ã使ç¨ããããã®è¨å®ã»å®è£
- åä½ç¢ºèª
- User æ å ±ãã»ããããã¯ã©ã¹ãæ°è¦ä½æã㦠Serializable ã¤ã³ã¿ã¼ãã§ã¼ã¹ã宣è¨ãã
- åä½ç¢ºèªï¼
- åä½ç¢ºèªï¼ ( ä»åº¦ã¯ Redis ã®ãã¼ã¿ãè¦ãªãã )
- åä½ç¢ºèªï¼ ( Redis ã®ãã¼ã¿ãæ¶ããã¨ã©ããªãã®ãï¼ )
- 次åã¯ããã
æé
ã¯ããã«
Spring Session ã使ç¨ãããã¨ã§ã»ãã·ã§ã³ãã¼ã¿ã Tomcat ã®ã¡ã¢ãªã§ã¯ãªã Redis ã§ç®¡çããããã«ãã¦ã¿ã¾ãã
ããã«ãã以ä¸ã®ä½æ¥ãä¸è¦ã«ãªã£ããå¹æãåºãããã¦ãã¹ã±ã¼ã«ã¢ã¦ããããå ´åã«ã¯åã« Tomcat ãå¢ããã¦è»¢éå ã¨ãã¦è¨å®ããã°ããã ãã«ãªãã¯ãã§ãã
- Apache ã Nginx ã® Web ãµã¼ãã§ã¹ãã£ããã¼ã»ãã·ã§ã³ã®è¨å®ããã¦ãã»ãã·ã§ã³ãçæãã Tomcat ã«ãªã¯ã¨ã¹ãã転éããå¿ è¦ããªããªãã¾ãã
- Tomcat éã§ã»ãã·ã§ã³ã¬ããªã±ã¼ã·ã§ã³ãããå¿ è¦ããªããªãã¾ãã
- Tomcat ãåèµ·åãããã»ãã·ã§ã³ãã¼ã¿ããªããªã£ã¦ãã°ã¢ã¦ãããããããã¨ããªããªãã¾ãã
1.0.x-use-spring-session ãã©ã³ãã®ä½æ
- IntelliJ IDEA 㧠1.0.x-use-spring-session ãã©ã³ããä½æãã¾ãã
Spring Session ã使ç¨ãã¦ããªãã¨ã©ããªãã®ãï¼
Spring Session ã使ç¨ãã¦ããªãä»ã®å®è£ ã ã¨ããã°ã¤ã³å¾ã« Tomcat ãåèµ·åãããã¨ãµã¼ãå´ã§ã¡ã¢ãªå ã«ä¿æãã¦ããã»ãã·ã§ã³æ å ±ãç ´æ£ãããããã次ã«ã¢ã¯ã»ã¹ããæã«ãã°ã¤ã³ç»é¢ã表示ããã¾ãã試ãã¦ã¿ã¾ãã
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åãã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ããID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ããèªè¨¼ã«æåãããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ããã¾ãã
Ctrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
ãã©ã¦ã¶ã§ãªãã¼ãããã¨ãã»ãã·ã§ã³æ å ±ãç ´æ£ããã¦ããããèªè¨¼ããã¦ããªãã¨å¤æããã¦ãã°ã¤ã³ç»é¢ã表示ããã¾ãã
Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
ã¾ãï¼å°ä»¥ä¸ Tomcat ãèµ·åãã¦ããç¶æ ã§ãã»ãã·ã§ã³ãçæãã Tomcat ã§ãªãæ¹ã® Tomcat ã«ãªã¯ã¨ã¹ããéä¿¡ãããã¨èªè¨¼ããã¦ããªãã¨å¤æããã¦ãã°ã¤ã³ç»é¢ã表示ããã¾ã ( ãã¡ãã¯è©¦ãã¾ãã )ã
Spring Session ã使ç¨ããããã®è¨å®ã»å®è£
Spring Session ã使ç¨ãããããã«ãã¾ãã
build.gradle ãå¤æ´ãã¦å¿ è¦ãªã©ã¤ãã©ãªããã¦ã³ãã¼ããã¾ããbuild.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsãã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦ãå¤æ´ãã build.gradle ã®å 容ãåæ ãã¾ãã
src/main/java/ksbysample/webapp/lending ã®ä¸ã® Application.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/resources ã®ä¸ã® application-develop.properties, application-unittest.properties, application-product.properties ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèª
åä½ç¢ºèªãã¾ãã
Redis 㯠Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( Redis ( Windows ç ) ã®ã¤ã³ã¹ãã¼ã« ) ã§ã¤ã³ã¹ãã¼ã«ãã¦ãµã¼ãã¹ã§èµ·åããã¾ã¾ã«ãã¦ãã¾ããããèµ·åãã¦ããªãå ´åã«ã¯èµ·åãã¾ãã
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åãã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ããID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ãã°ã¤ã³ã§ããã¨ã©ã¼ã«ãªãã¾ããããã
ãã°ãè¦ãã¨
Caused by: java.io.NotSerializableException: ksbysample.webapp.lending.entity.UserInfo
ã®ã¨ã©ã¼ãã°ãåºåããã¦ãã¾ãããRedis ã«ã»ãã·ã§ã³æ å ±ãä¿åããã®ã§ãä¿åããã¯ã©ã¹ã« Serializable ã¤ã³ã¿ã¼ãã§ã¼ã¹ã宣è¨ããå¿ è¦ããã£ãããã§ãã
User æ å ±ãã»ããããã¯ã©ã¹ãæ°è¦ä½æã㦠Serializable ã¤ã³ã¿ã¼ãã§ã¼ã¹ã宣è¨ãã
UserInfo ã¯ã©ã¹ã¯ DomaGen ãèªåçæãã Entity ã¯ã©ã¹ãªã®ã§ããã®ã¯ã©ã¹ã« Serializable ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯å®£è¨ãã¾ããã以ä¸ã®æ¹éã§å¤æ´ãã¾ãã
- LendingUser ã¯ã©ã¹ãæ°è¦ä½æãããã®ã¯ã©ã¹ã« Serializable ã¤ã³ã¿ã¼ãã§ã¼ã¹ã宣è¨ãã¾ãããã£ã¼ã«ã㯠UserInfo ã¯ã©ã¹ãã userId 以å¤ã®ãã®ãå ¨ã¦å®è£ ãã¾ãã
- ã¯ã©ã¹åã UserInfoUserDetails â LendingUserDetails ã¸ãUserInfoUserDetailsService â LendingUserDetailsService ã¸å¤æ´ãã¾ãã
- LendingUserDetails ã¯ã©ã¹ ( æ§ UserInfoUserDetails ã¯ã©ã¹ ) ã®ãã£ã¼ã«ãã®å ãUserInfo ã¯ã©ã¹ã LendingUser ã¯ã©ã¹ã¸å¤æ´ãã¾ãããã ã LendingUserDetails ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã¯ UserInfo ã¯ã©ã¹ ã®ã¾ã¾ã«ãã¾ã ( ã³ã³ã¹ãã©ã¯ã¿å 㧠UserInfo â LendingUser ã¸å¤æãã¾ã )ã
å®è£ ãã¾ãã
src/main/java/ksbysample/webapp/lending/security ã®ä¸ã« LendingUser.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/lending/security ã®ä¸ã® UserInfoUserDetails.java ã LendingUserDetails.java ã«ãªãã¼ã ãã¾ãããªãã¼ã å¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/lending/security ã®ä¸ã® UserInfoUserDetailsService.java ã LendingUserDetailsService.java ã«ãªãã¼ã ãã¾ãã
åä½ç¢ºèªï¼
Ctrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ããID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ä»åº¦ã¯ããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ããã¾ããã
Ctrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
ãã©ã¦ã¶ã§ãªãã¼ããã¾ãããã°ã¤ã³ç»é¢ã¸é·ç§»ãããå度ããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ããã¾ããã
ãã©ã¦ã¶ã§ http://localhost:8080/logout ã«ã¢ã¯ã»ã¹ããã°ã¢ã¦ããã¾ãã
ãã©ã¦ã¶ã§ http://localhost:8080/loginsuccess ã«ã¢ã¯ã»ã¹ãã¾ãããã°ã¢ã¦ãã§ã»ãã·ã§ã³æ å ±ãåé¤ããã¦ããã®ã§ãä»åº¦ã¯ãã°ã¤ã³ç»é¢ã«é·ç§»ãã¾ããã
Ctrl+F5 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
åä½ç¢ºèªï¼ ( ä»åº¦ã¯ Redis ã®ãã¼ã¿ãè¦ãªãã )
Resis ã«ãã¼ã¿ãã©ã®ããã«ä½æããã¦ããã®ã確èªãã¾ãã
ãã©ã¦ã¶ãèµ·åãã¦ãããä¸æ¦çµäºããã¾ãã
ã³ãã³ãã©ã¤ã³ãã redis-cli ã³ãã³ã㧠Redis ä¸ã®ãã¼ã¿ãã¯ãªã¢ãã¾ãã
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åãã¾ãã
ãã©ã¦ã¶ãèµ·åã㦠http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ãã
Redis ä¸ã®ãã¼ã¿ã確èªããã¨ãã¼ã¿ãä½æããã¦ãã¾ããããã°ã¤ã³ç»é¢ã«ã¢ã¯ã»ã¹ããæç¹ã§ã»ãã·ã§ã³ ID ãçºè¡ããã¦ããããã§ãã
ID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ããèªè¨¼ã«æåãããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ããã¾ãã
Redis ä¸ã®ãã¼ã¿ã確èªããã¨
"spring:session:sessions:d2b3e547-0bf1-497a-9807-de124adfe016"
ãæ¶ãã¦"spring:session:sessions:59ca4193-6ffc-4b84-be1a-0e817d1526fe"
ã¨"spring:session:expirations:1437824400000"
ã®ãã¼ã¿ã追å ããã¦ãã¾ãããã»ãã·ã§ã³ ID ãåé¤ããå¾ãæ°è¦ã»ãã·ã§ã³ ID ãçºè¡ãããããã§ããCtrl+F5 ãæ¼ã㦠Tomcat ãåèµ·åãã¾ãã
Redis ã®ãã¼ã¿ã¯ä½ãå¤ããã¾ããã§ããã
ãã©ã¦ã¶ã§ãªãã¼ããã¦ããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ãç´ãã¾ãã
Redis ã®ãã¼ã¿ã¯
"spring:session:expirations:1437824400000"
ãæ¶ãã¦"spring:session:expirations:1437824820000"
ã追å ããã¦ãã¾ããããã©ã¦ã¶ã§ http://localhost:8080/logout ã«ã¢ã¯ã»ã¹ããã°ã¢ã¦ããã¦ããã°ã¤ã³ç»é¢ã«æ»ãã¾ãã
Redis ã®ãã¼ã¿ã¯
"spring:session:sessions:59ca4193-6ffc-4b84-be1a-0e817d1526fe"
ãåé¤ããã¦"spring:session:sessions:e4886eba-d984-4349-99d7-8435e30ccc05"
ã¨"spring:session:expirations:1437824940000"
ã追å ããã¦ãã¾ããããã°ã¢ã¦ãæã«ã»ãã·ã§ã³ ID ãåé¤ããã¾ããããã°ã¤ã³ç»é¢ã«ã¢ã¯ã»ã¹ããæã«æ°è¦ã»ãã·ã§ã³ ID ãçºè¡ãããããã§ããCtrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
Redis ã®ãã¼ã¿ã¯å¤ããã¾ããã§ããã
ãã°ã¤ã³ç»é¢ã«ã¢ã¯ã»ã¹ããæã«ã¾ã§ã»ãã·ã§ã³ ID ãçºè¡ããªãã¦ãããã®ã§ã¯ï¼ãã¨æãã¾ãããã表示ããããã°ã¤ã³ç»é¢ã®ã½ã¼ã¹ãè¦ãã
<input type="hidden" name="_csrf" value="c1b41fc6-7375-43e8-88e1-d6e624797a5f" />
ãåºåããã¦ãã¾ãããCSRF 対çã®ããã®ãµã¼ãå´ãã¼ã¿ãä¿æããããã«ã»ãã·ã§ã³ ID ãçºè¡ããã¦ããããã§ã ( Spring Session ã§ã¯ãªã Spring Security ã®æ©è½ã§ã )ãCSRF 対çãéè¦ã§ããããã°ã¤ã³ç»é¢ã«ã¢ã¯ã»ã¹ããã度ã«ã»ãã·ã§ã³ãã¼ã¿ã Redis ã«ä½æãããã®ã¯ä½ãããã§ããããã
åä½ç¢ºèªï¼ ( Redis ã®ãã¼ã¿ãæ¶ããã¨ã©ããªãã®ãï¼ )
Redis ã®ãã¼ã¿ãæåã§åé¤ããã¨ã©ã®ãããªæåã«ãªãã確èªãã¦ã¿ã¾ãã
ãã©ã¦ã¶ãèµ·åãã¦ãããä¸æ¦çµäºããã¾ãã
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åãã¾ãã
ãã©ã¦ã¶ãèµ·åã㦠http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ãã
ã³ãã³ãã©ã¤ã³ãã redis-cli ã³ãã³ã㧠Redis ä¸ã®ãã¼ã¿ãã¯ãªã¢ãã¾ãã
ID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ãã
ã¨ã©ã¼ç»é¢ã表示ããã¾ãããç»é¢ä¸ã«
Expected CSRF token not found. Has your session expired?
ã¨è¡¨ç¤ºããã¦ããéãããµã¼ãã® CSRF ãã¼ã¯ã³ãåé¤ããã®ãåå ã§ãããå度ãã©ã¦ã¶ã§ http://localhost:8080/ ã«ã¢ã¯ã»ã¹ãããã°ã¤ã³ç»é¢ã表示ãã¾ããID ã« "[email protected]"ãPassword ã« "taro" ãå ¥åããã次åããèªåçã«ãã°ã¤ã³ãã ãã¯ãã§ãã¯ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ãã¾ããèªè¨¼ã«æåãããã°ã¤ã³æåï¼ãã®ç»é¢ã表示ããã¾ãã
Redis ä¸ã®ãã¼ã¿ãã¯ãªã¢ãã¾ãã
ãã©ã¦ã¶ã§ãªãã¼ããã¾ãããµã¼ãå´ã®ã»ãã·ã§ã³ãã¼ã¿ãåé¤ãã¦ããã®ã§ãã°ã¤ã³ç»é¢ã¸é·ç§»ãã¾ããã
Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
次åã¯ããã
Spring Session ã使ç¨ããå ´åã®æ¤è¨¼ãç¶ãã¾ãã以ä¸ã®æ¤è¨¼ãããäºå®ã§ããã¾ãä»åå¤æ´ãã¦ããã½ã¼ã¹ã¯æ¬¡åãã¼ã¸ãã¾ãã
- ãã°ã¤ã³ç»é¢åã³ãã°ã¤ã³ URL ( /login ) 㧠CSRF 対çãå¤ãæ¹æ³ãããã®ãï¼
- Redis ãè¤æ°ç¨æããå ´åã試ãã¦ã¿ãã
ã½ã¼ã¹ã³ã¼ã
build.gradle
dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4-1201-jdbc41" // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ããããã® // Appendix E. Dependency versions ( http://docs.spring.io/spring-boot/docs/current/reference/html/appendix-dependency-versions.html ) åç § compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-velocity") compile("org.springframework.boot:spring-boot-starter-mail") compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-redis") compile("org.codehaus.janino:janino") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.springframework.security:spring-security-test:4.0.1.RELEASE") testCompile("org.yaml:snakeyaml") // spring-boot-gradle-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã® compile("${jdbcDriver}") compile("org.seasar.doma:doma:2.3.1") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.apache.commons:commons-lang3:3.4") compile("org.projectlombok:lombok:1.16.4") compile("com.google.guava:guava:18.0") compile("org.springframework.session:spring-session:1.0.1.RELEASE") testCompile("org.dbunit:dbunit:2.5.1") testCompile("com.icegreen:greenmail:1.4.1") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.3.1") domaGenRuntime("${jdbcDriver}") }
compile("org.springframework.boot:spring-boot-starter-redis")
ã追å ãã¾ããcompile("org.springframework.session:spring-session:1.0.1.RELEASE")
ã追å ãã¾ãã
Application.java
package ksbysample.webapp.lending; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import java.text.MessageFormat; @ImportResource("classpath:applicationContext-${spring.profiles.active}.xml") @SpringBootApplication @EnableRedisHttpSession public class Application { public static void main(String[] args) { String springProfilesActive = System.getProperty("spring.profiles.active"); if (!StringUtils.equals(springProfilesActive, "product") && !StringUtils.equals(springProfilesActive, "develop") && !StringUtils.equals(springProfilesActive, "unittest")) { throw new UnsupportedOperationException(MessageFormat.format("JVMã®èµ·åæå¼æ° -Dspring.profiles.active 㧠develop ã unittest ã product ãæå®ãã¦ä¸ãã ( -Dspring.profiles.active={0} )ã", springProfilesActive)); } SpringApplication.run(Application.class, args); } }
@EnableRedisHttpSession
ã追å ãã¾ãã
application-develop.properties, application-unittest.properties, application-product.properties
â application-develop.properties
spring.datasource.url=jdbc:log4jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy spring.mail.host=localhost spring.mail.port=25 spring.redis.host=localhost spring.redis.port=6379 spring.messages.cache-seconds=0 spring.thymeleaf.cache=false spring.velocity.cache=false
spring.redis.host=localhost
,spring.redis.port=6379
ã追å ãã¾ãããã ããã®ï¼ã¤ã®è¨å®ã¯ããã©ã«ã㧠localhost, 6379 ãè¨å®ããã¦ãããRedis ãèµ·åãã¦ããã®ã localhost:6379 ãªã®ã§ããã°è¨å®ããªãã¦ãåä½ãã¾ãã
â application-unittest.properties
spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.redis.host=localhost spring.redis.port=6379 spring.thymeleaf.cache=true
spring.redis.host=localhost
,spring.redis.port=6379
ã追å ãã¾ãã
â application-product.properties
server.tomcat.basedir=C:/webapps/ksbysample-webapp-lending spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.redis.host=localhost spring.redis.port=6379 spring.thymeleaf.cache=true
spring.redis.host=localhost
,spring.redis.port=6379
ã追å ãã¾ãã
LendingUser.java
package ksbysample.webapp.lending.security; import ksbysample.webapp.lending.entity.UserInfo; import lombok.Data; import org.springframework.beans.BeanUtils; import java.io.Serializable; import java.time.LocalDateTime; @Data public class LendingUser implements Serializable { String username; String password; String mailAddress; Short enabled; Short cntBadcredentials; LocalDateTime expiredAccount; LocalDateTime expiredPassword; public LendingUser(UserInfo userInfo) { BeanUtils.copyProperties(userInfo, this); } }
LendingUserDetails.java
package ksbysample.webapp.lending.security; import ksbysample.webapp.lending.entity.UserInfo; import org.springframework.beans.BeanUtils; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import java.time.LocalDateTime; import java.util.Collection; import java.util.Set; public class LendingUserDetails implements UserDetails { private LendingUser lendingUser; private final Set<? extends GrantedAuthority> authorities; private final boolean accountNonExpired; private final boolean accountNonLocked; private final boolean credentialsNonExpired; private final boolean enabled; public LendingUserDetails(UserInfo userInfo , Set<? extends GrantedAuthority> authorities) { LocalDateTime now = LocalDateTime.now(); lendingUser = new LendingUser(userInfo); this.authorities = authorities; this.accountNonExpired = !userInfo.getExpiredAccount().isBefore(now); this.accountNonLocked = (userInfo.getCntBadcredentials() < 5); this.credentialsNonExpired = !userInfo.getExpiredPassword().isBefore(now); this.enabled = (userInfo.getEnabled() == 1); } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return authorities; } @Override public String getPassword() { return lendingUser.getPassword(); } @Override public String getUsername() { return lendingUser.getMailAddress(); } public String getName() { return lendingUser.getUsername(); } @Override public boolean isAccountNonExpired() { return accountNonExpired; } @Override public boolean isAccountNonLocked() { return accountNonLocked; } @Override public boolean isCredentialsNonExpired() { return credentialsNonExpired; } @Override public boolean isEnabled() { return enabled; } }
private UserInfo userInfo
âprivate LendingUser lendingUser;
ã¸å¤æ´ãã¾ãã- ã³ã³ã¹ãã©ã¯ã¿å
ã®å¦çã
this.userInfo = userInfo;
âlendingUser = new LendingUser(userInfo);
ã¸å¤æ´ãã¾ãã
å±¥æ´
2015/07/26
åççºè¡ã