├── spring-boot-application-https ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── application │ │ └── https │ │ └── SpringHttpsApp.java │ └── sources │ ├── application.properties │ └── ssl.p12 ├── spring-boot-security-form-auth-remember-persistence-token ├── build.gradle ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── form │ │ └── auth │ │ └── remember │ │ └── persistence │ │ └── token │ │ ├── SpringSecurityFormJdbcAuthRememberMeApp.java │ │ ├── SpringSecurityFormJdbcAuthRememberMeApp.java_3 │ │ ├── config │ │ ├── DatabaseConfig.java │ │ ├── PasswordEncryptorConfig.java_3 │ │ ├── SecurityConfig.jav │ │ └── SpringSecurity.java_3 │ │ ├── controller │ │ ├── SpringSecurityController.java │ │ └── SpringSecurityController.java_3 │ │ ├── dao │ │ └── UserDao.java_3 │ │ ├── model │ │ └── User.java_3 │ │ ├── rowmapper │ │ └── UserRowMapper.java_3 │ │ └── service │ │ └── UserAuthService.java_3 │ ├── resources │ ├── application.properties │ ├── application.properties_3 │ ├── persistent_logins.sql │ ├── static │ │ └── css │ │ │ └── style.css │ ├── templates │ │ ├── admin.html │ │ ├── home.html │ │ └── login.html │ ├── user-role.sql │ └── user.sql │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-boot-security-form-auth-remember ├── build.gradle ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── form │ │ └── auth │ │ └── remember │ │ ├── SpringSecurityFormAuthRememberMeApp.java │ │ ├── SpringSecurityFormAuthRememberMeApp.java_3 │ │ ├── config │ │ ├── SecurityConfig.java │ │ └── SecurityConfig.java_3 │ │ └── controller │ │ ├── SpringSecurityController.java │ │ └── SpringSecurityController.java_3 │ ├── resources │ ├── application.properties │ ├── static │ │ └── css │ │ │ └── style.css │ └── templates │ │ ├── admin.html │ │ ├── home.html │ │ └── login.html │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-boot-security-form-based-authentication ├── build.gradle ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── form │ │ └── based │ │ └── authentication │ │ ├── SpringSecurityFormBasedAuthApp.java │ │ ├── SpringSecurityFormBasedAuthApp.java_3 │ │ ├── config │ │ ├── PasswordEncryptorConfig.java │ │ ├── SecurityConfig.java │ │ └── SecurityConfig.java_3 │ │ └── controller │ │ ├── SpringSecurityController.java │ │ └── SpringSecurityController.java_3 │ ├── resources │ ├── application.properties │ ├── static │ │ └── css │ │ │ └── style.css │ └── templates │ │ ├── admin.html │ │ ├── home.html │ │ └── login.html │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-boot-security-form-based-jdbc-authentication ├── build.gradle ├── mysql_sql.sql ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── form │ │ └── based │ │ └── jdbc │ │ └── authentication │ │ ├── SpringSecurityFormBasedJdbcAuthApp.java │ │ ├── SpringSecurityFormBasedJdbcAuthApp.java_3 │ │ ├── config │ │ ├── DatabaseConfig.java │ │ ├── PasswordEncryptorConfig.java_3 │ │ ├── SecurityConfig.java │ │ └── SecurityConfig.java_3 │ │ └── controller │ │ ├── SpringSecurityController.java │ │ └── SpringSecurityController.java_3 │ ├── resources │ ├── application.properties │ ├── application.properties_3 │ ├── static │ │ └── css │ │ │ └── style.css │ ├── templates │ │ ├── admin.html │ │ ├── home.html │ │ └── login.html │ ├── user-role.sql │ └── user.sql │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-boot-security-form-based-jdbc-userdetailsservice-auth ├── build.gradle ├── pom.xml ├── pom.xml_3 ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── form │ │ └── based │ │ └── jdbc │ │ └── userdetailsservice │ │ └── auth │ │ ├── App.java │ │ ├── config │ │ ├── DatabaseConfig.java │ │ ├── EncoderConfig.java │ │ ├── SecurityConfig.java │ │ ├── SecurityConfig.java_2.7.4 │ │ └── SecurityConfig.java_3 │ │ ├── controller │ │ ├── SpringSecurityController.java │ │ └── SpringSecurityController.java_3 │ │ ├── dao │ │ ├── UserDao.java │ │ └── UserDao.java_2.7.4 │ │ ├── model │ │ └── User.java │ │ ├── rowmapper │ │ └── UserRowMapper.java │ │ └── service │ │ └── UserAuthService.java │ ├── resources │ ├── application.properties │ ├── application.properties_3 │ ├── static │ │ └── css │ │ │ └── style.css │ ├── templates │ │ ├── admin.html │ │ ├── home.html │ │ └── login.html │ ├── user-role.sql │ ├── user.sql │ └── user.sql_2.7.4 │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-boot-security-jwt-auth-api ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── security │ │ └── jwt │ │ └── auth │ │ └── api │ │ ├── SpringSecurityJwtApiApp.java │ │ ├── config │ │ ├── ApiAuthenticationEntryPoint.java │ │ └── ApiSecurityConfig.java │ │ ├── entity │ │ ├── User.java │ │ └── UserRole.java │ │ ├── exception │ │ ├── DisabledUserException.java │ │ ├── InvalidUserCredentialsException.java │ │ ├── JwtTokenMalformedException.java │ │ └── JwtTokenMissingException.java │ │ ├── filter │ │ └── JwtAuthenticationFilter.java │ │ ├── repository │ │ └── UserRepository.java │ │ ├── rest │ │ └── controller │ │ │ ├── GreetingRestApi.java │ │ │ └── JwtRestApi.java │ │ ├── service │ │ └── UserAuthService.java │ │ ├── util │ │ └── JwtUtil.java │ │ └── vo │ │ ├── Request.java │ │ └── Response.java │ └── resources │ └── application.properties ├── spring-jasypt-enableencryptableproperties ├── build.gradle ├── pom.xml ├── pom.xml_3 ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── jasypt │ │ │ └── enableencryptableproperties │ │ │ ├── JasyptEnableEncryptablePropertiesApp.java │ │ │ ├── config │ │ │ └── Config.java │ │ │ ├── model │ │ │ ├── User.java │ │ │ └── User.java_3 │ │ │ ├── repository │ │ │ └── UserJpaRepository.java │ │ │ └── service │ │ │ └── UserService.java │ │ └── resources │ │ ├── application.properties │ │ └── application.properties_3 └── user.sql ├── spring-oauth2-sso-aws-cognito ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── oauth2 │ │ └── sso │ │ ├── OAuth2SsoSecurityConfig.java │ │ └── SpringOAuth2SsoApp.java │ └── resources │ ├── application.yml │ └── templates │ └── index.html ├── spring-preauthorize-has-permission-data-jpa ├── pom.xml ├── pom.xml_3_2_5 ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── preauthorize │ │ │ └── haspermission │ │ │ └── data │ │ │ └── jpa │ │ │ ├── App.java │ │ │ ├── config │ │ │ ├── CustomPermissionEvaluator.java │ │ │ ├── EncoderConfig.java │ │ │ ├── PermissionConfig.java │ │ │ ├── SecurityConfig.java │ │ │ └── SecurityConfig.java_3_2_5 │ │ │ ├── entity │ │ │ ├── User.java │ │ │ └── UserRole.java │ │ │ ├── repository │ │ │ ├── UserRepository.java │ │ │ └── UserRoleRepository.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── AppRestController.java │ │ │ └── service │ │ │ └── UserAuthService.java │ │ └── resources │ │ └── application.properties └── user.sql ├── spring-preauthorize-hasrole-data-jpa ├── pom.xml ├── pom.xml_3 ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── preauthorize │ │ └── hasrole │ │ └── data │ │ └── jpa │ │ ├── App.java │ │ ├── App.java_3 │ │ ├── config │ │ ├── EncoderConfig.java │ │ ├── SecurityConfig.java │ │ └── SecurityConfig.java_3 │ │ ├── entity │ │ ├── User.java │ │ ├── User.java_3 │ │ ├── UserRole.java │ │ └── UserRole.java_3 │ │ ├── repository │ │ ├── UserRepository.java │ │ └── UserRoleRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── AppRestController.java │ │ └── service │ │ └── UserAuthService.java │ └── resources │ └── application.properties ├── spring-security-annotations ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── security │ │ └── annotations │ │ ├── config │ │ ├── MvcWebApplicationInitializer.java │ │ ├── SecurityWebApplicationInitializer.java │ │ ├── WebMvcConfig.java │ │ └── WebSecurityConfig.java │ │ └── controllers │ │ └── SpringSecurityController.java │ ├── resources │ └── messages.properties │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-security-data-jpa-pre-authentication ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── security │ │ └── data │ │ └── jpa │ │ └── preauthentication │ │ ├── PreAuthSecurityApp.java │ │ ├── config │ │ └── SecurityConfig.java │ │ ├── entity │ │ ├── User.java │ │ └── UserRole.java │ │ ├── repository │ │ ├── UserRepository.java │ │ └── UserRoleRepository.java │ │ ├── rest │ │ └── controller │ │ │ └── BlogRestController.java │ │ ├── service │ │ └── CustomUserDetailsService.java │ │ └── vo │ │ └── BlogVo.java │ └── resources │ ├── application.properties │ ├── data.sql │ └── schema.sql ├── spring-security-jdbc-authentication-form-based-remember-me ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── security │ │ │ └── jdbc │ │ │ └── authentication │ │ │ └── formbased │ │ │ └── rememberme │ │ │ ├── config │ │ │ ├── MvcWebApplicationInitializer.java │ │ │ ├── SecurityWebApplicationInitializer.java │ │ │ ├── WebMvcConfig.java │ │ │ └── WebSecurityConfig.java │ │ │ ├── controllers │ │ │ └── SpringSecurityController.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ ├── model │ │ │ └── UserDetails.java │ │ │ ├── rowmapper │ │ │ └── UserRowMapper.java │ │ │ └── service │ │ │ └── UserAuthService.java │ │ ├── resources │ │ ├── jdbc.properties │ │ └── messages.properties │ │ └── webapp │ │ ├── static │ │ └── css │ │ │ └── style.css │ │ └── views │ │ ├── admin.jsp │ │ ├── index.jsp │ │ └── login.jsp └── user.sql ├── spring-security-jdbc-authentication-persistent-token-remember-me ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── com │ │ └── roytuts │ │ │ └── spring │ │ │ └── security │ │ │ └── jdbc │ │ │ └── authentication │ │ │ └── persistent │ │ │ └── rememberme │ │ │ ├── config │ │ │ ├── MvcWebApplicationInitializer.java │ │ │ ├── SecurityWebApplicationInitializer.java │ │ │ ├── WebMvcConfig.java │ │ │ └── WebSecurityConfig.java │ │ │ ├── controllers │ │ │ └── SpringSecurityController.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ ├── model │ │ │ └── UserDetails.java │ │ │ ├── rowmapper │ │ │ └── UserRowMapper.java │ │ │ └── service │ │ │ └── UserAuthService.java │ │ ├── resources │ │ ├── jdbc.properties │ │ └── messages.properties │ │ └── webapp │ │ ├── static │ │ └── css │ │ │ └── style.css │ │ └── views │ │ ├── admin.jsp │ │ ├── index.jsp │ │ └── login.jsp └── user.sql ├── spring-security-jdbc-authentication-userdetailsservice ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── security │ │ └── jdbc │ │ └── authentication │ │ ├── config │ │ ├── MvcWebApplicationInitializer.java │ │ ├── SecurityWebApplicationInitializer.java │ │ ├── WebMvcConfig.java │ │ └── WebSecurityConfig.java │ │ ├── controllers │ │ └── SpringSecurityController.java │ │ ├── dao │ │ └── UserDao.java │ │ ├── model │ │ └── UserDetails.java │ │ ├── rowmapper │ │ └── UserRowMapper.java.java │ │ └── service │ │ └── UserAuthService.java │ ├── resources │ ├── jdbc.properties │ └── messages.properties │ └── webapp │ ├── static │ └── css │ │ └── style.css │ └── views │ ├── admin.jsp │ ├── index.jsp │ └── login.jsp ├── spring-security-jdbc-authentication ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── security │ │ │ └── jdbc │ │ │ └── authentication │ │ │ ├── config │ │ │ ├── MvcWebApplicationInitializer.java │ │ │ ├── SecurityWebApplicationInitializer.java │ │ │ ├── WebMvcConfig.java │ │ │ └── WebSecurityConfig.java │ │ │ └── controllers │ │ │ └── SpringSecurityController.java │ │ ├── resources │ │ ├── jdbc.properties │ │ └── messages.properties │ │ └── webapp │ │ ├── static │ │ └── css │ │ │ └── style.css │ │ └── views │ │ ├── admin.jsp │ │ ├── index.jsp │ │ └── login.jsp └── user.sql ├── spring-security-pre-authentication ├── build.gradle ├── pom.xml ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── spring │ │ └── security │ │ └── preauth │ │ ├── SpringPreAuthentication.java │ │ ├── config │ │ ├── PreAuthenticationSecurityConfig.java │ │ └── SpringPreAuthenticationConfig.java │ │ ├── dao │ │ └── UserDao.java │ │ ├── jdbc │ │ └── row │ │ │ └── mapper │ │ │ └── UserRowMapper.java │ │ ├── model │ │ └── User.java │ │ ├── rest │ │ └── controller │ │ │ └── UserRestController.java │ │ ├── service │ │ └── CustomUserDetailsService.java │ │ └── vo │ │ └── BlogVo.java │ └── resources │ ├── application.properties │ ├── create-table.sql │ └── insert-data.sql ├── spring-security-preauthorize-has-permission ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── security │ │ │ └── preauth │ │ │ ├── SpringSecurityPreauthHasPermissionApp.java │ │ │ ├── config │ │ │ ├── PasswordConfig.java │ │ │ └── SpringPreAuthorizeSecurityConfig.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ ├── model │ │ │ ├── Role.java │ │ │ └── User.java │ │ │ ├── permission │ │ │ └── CustomPermissionEvaluator.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── PreAuthorizeRestController.java │ │ │ ├── row │ │ │ └── mapper │ │ │ │ └── UserRowMapper.java │ │ │ └── service │ │ │ └── UserAuthService.java │ │ └── resources │ │ └── application.properties └── user_role.sql ├── spring-security-preauthorize-has-role ├── build.gradle ├── pom.xml ├── readme.rst ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── security │ │ │ └── preauth │ │ │ ├── SpringSecurityPreauthHasRoleApp.java │ │ │ ├── config │ │ │ ├── DatabaseConfig.java │ │ │ ├── EncoderConfig.java │ │ │ ├── SpringPreAuthorizeSecurityConfig.java │ │ │ └── SpringPreAuthorizeSecurityConfig_2.7.5.java │ │ │ ├── dao │ │ │ ├── UserDao.java │ │ │ └── UserDao_2.7.5.java │ │ │ ├── model │ │ │ ├── Role.java │ │ │ └── User.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── PreAuthorizeRestController.java │ │ │ ├── row │ │ │ └── mapper │ │ │ │ └── UserRowMapper.java │ │ │ └── service │ │ │ └── UserAuthService.java │ │ └── resources │ │ └── application.properties └── user_role.sql ├── spring-security-simple-auth ├── pom.xml ├── readme.rst └── src │ └── main │ └── java │ └── com │ └── roytuts │ └── springsecurity │ └── simpleauth │ ├── App.java │ ├── config │ ├── EncoderConfig.java │ └── SecurityConfig.java │ └── restcontroller │ └── AppRestController.java ├── springboot-angular-security ├── build.gradle ├── pom.xml ├── pom.xml_3 ├── readme.rst └── src │ └── main │ ├── java │ └── com │ │ └── roytuts │ │ └── springboot │ │ └── angularsecurity │ │ ├── SpringBootAngularSecurityApp.java │ │ ├── config │ │ ├── SpringSecurityConfig.java │ │ └── SpringSecurityConfig.java_3 │ │ └── rest │ │ └── controller │ │ └── UiRestController.java │ └── resources │ └── application.properties └── springboot-react-security ├── pom.xml ├── readme.rst └── src └── main ├── java └── com │ └── roytuts │ └── springboot │ └── reactsecurity │ ├── SpringBootReactSecurityApp.java │ ├── config │ └── SpringSecurityConfig.java │ └── rest │ └── controller │ └── UiRestController.java └── resources └── application.properties /spring-boot-application-https/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-boot-application-https 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.4 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-maven-plugin 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-boot-application-https/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/spring-boot-application-over-https/ -------------------------------------------------------------------------------- /spring-boot-application-https/src/main/java/com/roytuts/spring/boot/application/https/SpringHttpsApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.application.https; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @SpringBootApplication 12 | public class SpringHttpsApp { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(SpringHttpsApp.class, args); 16 | } 17 | 18 | @GetMapping("/") 19 | public ResponseEntity home() { 20 | return new ResponseEntity("Welcome to self-certificate https site", HttpStatus.OK); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-application-https/src/main/sources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8443 2 | server.ssl.enabled=true 3 | server.ssl.key-alias=selfsigned 4 | server.ssl.key-store=classpath:ssl.p12 5 | server.ssl.key-store-type=PKCS12 6 | server.ssl.key-store-password=changeit -------------------------------------------------------------------------------- /spring-boot-application-https/src/main/sources/ssl.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-security/64b0571c4be61410846d2a44c2e85b9aead2f4f9/spring-boot-application-https/src/main/sources/ssl.p12 -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.6.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | implementation("org.springframework:spring-jdbc:5.1.8.RELEASE") 29 | implementation('org.apache.tomcat.embed:tomcat-embed-jasper:9.0.22') 30 | implementation('javax.servlet:jstl:1.2') 31 | runtime("com.h2database:h2:1.4.196") 32 | } -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-form-based-authentication-persistence-token-remember-me/ 2 | 3 | For spring boot 3 the HTML pages and css file have been kept under src/main/resources folder and templates & static/css sub-folders. The java files are appended with _3 for spring boot 3. 4 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/SpringSecurityFormJdbcAuthRememberMeApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.boot.security.form.auth.remember.persistence.token") 7 | public class SpringSecurityFormJdbcAuthRememberMeApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormJdbcAuthRememberMeApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/SpringSecurityFormJdbcAuthRememberMeApp.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityFormJdbcAuthRememberMeApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormJdbcAuthRememberMeApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/config/DatabaseConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 9 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 10 | import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl; 11 | import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; 12 | 13 | @Configuration 14 | public class DatabaseConfig { 15 | 16 | @Bean 17 | public DataSource dataSource() { 18 | EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 19 | EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) // .H2 or .DERBY, etc. 20 | .addScript("user.sql").addScript("user-role.sql").addScript("persistent_logins.sql").build(); 21 | return db; 22 | } 23 | 24 | @Bean 25 | public PersistentTokenRepository persistentTokenRepository() { 26 | JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl(); 27 | tokenRepository.setDataSource(dataSource()); 28 | return tokenRepository; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/config/PasswordEncryptorConfig.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class PasswordEncryptorConfig { 10 | 11 | @Bean 12 | public PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/dao/UserDao.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jdbc.core.JdbcTemplate; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import com.roytuts.spring.boot.security.form.auth.remember.persistence.token.model.User; 8 | import com.roytuts.spring.boot.security.form.auth.remember.persistence.token.rowmapper.UserRowMapper; 9 | 10 | @Repository 11 | public class UserDao { 12 | 13 | @Autowired 14 | private JdbcTemplate jdbcTemplate; 15 | 16 | public User getUser(final String username) { 17 | 18 | return jdbcTemplate.queryForObject( 19 | "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name", 20 | new UserRowMapper(), username); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/model/User.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token.model; 2 | 3 | public class User { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public User() { 10 | } 11 | 12 | public User(String username, String password, String role) { 13 | this.username = username; 14 | this.password = password; 15 | this.role = role; 16 | } 17 | 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | public void setUsername(String username) { 23 | this.username = username; 24 | } 25 | 26 | public String getPassword() { 27 | return password; 28 | } 29 | 30 | public void setPassword(String password) { 31 | this.password = password; 32 | } 33 | 34 | public String getRole() { 35 | return role; 36 | } 37 | 38 | public void setRole(String role) { 39 | this.role = role; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/persistence/token/rowmapper/UserRowMapper.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.persistence.token.rowmapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.boot.security.form.auth.remember.persistence.token.model.User; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public User mapRow(ResultSet rs, int rowNum) throws SQLException { 14 | return new User(rs.getString("user_name"), rs.getString("user_pass"), rs.getString("user_role")); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/views/ 2 | spring.mvc.view.suffix=.jsp -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/application.properties_3: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | logging.level.org.springframework.jdbc.core = TRACE #log SQL 8 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/persistent_logins.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `persistent_logins`; 2 | 3 | CREATE TABLE `persistent_logins` ( 4 | `username` varchar(64) NOT NULL, 5 | `series` varchar(64) NOT NULL, 6 | `token` varchar(64) NOT NULL, 7 | `last_used` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 8 | PRIMARY KEY (`series`) 9 | ); -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | .msg { 11 | padding: 15px; 12 | margin-bottom: 20px; 13 | border: 1px solid transparent; 14 | border-radius: 4px; 15 | color: #31708f; 16 | background-color: #d9edf7; 17 | border-color: #bce8f1; 18 | } 19 | #login-box { 20 | width: 500px; 21 | padding: 20px; 22 | margin: 50px auto; 23 | background: #fff; 24 | -webkit-border-radius: 2px; 25 | -moz-border-radius: 2px; 26 | border: 1px solid #000; 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/templates/admin.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security Basic - Form Based Authentication 6 | 7 | 8 |
9 |

10 |

11 |

12 | Welcome 13 | | Logout 14 |

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 7 |
8 |

Home Page

9 | Go to Administrator page 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/user-role.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user_role` */ 2 | 3 | DROP TABLE IF EXISTS `user_role`; 4 | 5 | CREATE TABLE `user_role` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_role` varchar(15) NOT NULL, 8 | FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 9 | ); 10 | 11 | /*Data for the table `user_role` */ 12 | 13 | insert into `user_role`(`user_name`,`user_role`) values ('admin','ROLE_ADMIN'); -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/resources/user.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user` */ 2 | 3 | DROP TABLE IF EXISTS `user`; 4 | 5 | CREATE TABLE `user` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_pass` varchar(255) NOT NULL, 8 | `enable` tinyint(1) NOT NULL DEFAULT '1', 9 | PRIMARY KEY (`user_name`) 10 | ); 11 | 12 | /*Data for the table `user` */ 13 | 14 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('admin','$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK',1); -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | Spring Security Basic - Form Based Authentication 8 | 9 | 10 |
11 |

${title}

12 |

${message}

13 | 14 |

15 | Welcome 16 | : ${pageContext.request.userPrincipal.name} | Logout 18 |

19 |
20 |
21 | 22 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | 8 | 9 |
10 |

Home Page

11 | Go to Administrator page 12 |
13 | 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember-persistence-token/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | " /> 9 | 10 | 11 |
12 |

Login Here

13 | 14 |
${error}
15 |
16 | 17 |
${msg}
18 |
19 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 |
User:
Password:
Remember Me:
39 | 41 |
42 |
43 | 44 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.6.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | compile('org.apache.tomcat.embed:tomcat-embed-jasper:9.0.22') 29 | compile('javax.servlet:jstl:1.2') 30 | } -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-form-based-authentication-remember-me/ 2 | 3 | The java files for spring boot 3 are appended with _3. The static resources (HTML/CSS) are kept under src/main/resources folder and templates & static/css sub-folders. 4 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/SpringSecurityFormAuthRememberMeApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.boot.security.form.auth.remember") 7 | public class SpringSecurityFormAuthRememberMeApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormAuthRememberMeApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/SpringSecurityFormAuthRememberMeApp.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityFormAuthRememberMeApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormAuthRememberMeApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/java/com/roytuts/spring/boot/security/form/auth/remember/controller/SpringSecurityController.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.auth.remember.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | import jakarta.servlet.http.HttpServletRequest; 9 | 10 | @Controller 11 | public class SpringSecurityController { 12 | 13 | @GetMapping("/") 14 | public String defaultPage(Model model) { 15 | model.addAttribute("msg", "Welcome to Spring Security"); 16 | return "home"; 17 | } 18 | 19 | @GetMapping("/login") 20 | public String loginPage(Model model, @RequestParam(value = "error", required = false) String error, 21 | @RequestParam(value = "logout", required = false) String logout) { 22 | if (error != null) { 23 | model.addAttribute("error", "Invalid Credentials"); 24 | } 25 | if (logout != null) { 26 | model.addAttribute("msg", "You have been successfully logged out"); 27 | } 28 | return "login"; 29 | } 30 | 31 | @GetMapping("/logout") 32 | public String logoutPage(Model model, HttpServletRequest request) { 33 | request.getSession().invalidate(); 34 | return "redirect:/login?logout"; 35 | } 36 | 37 | @GetMapping("/admin") 38 | public String adminPage(Model model) { 39 | model.addAttribute("title", "Administrator Control Panel"); 40 | model.addAttribute("message", "This page demonstrates how to use Spring security"); 41 | return "admin"; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/views/ 2 | spring.mvc.view.suffix=.jsp -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | .msg { 11 | padding: 15px; 12 | margin-bottom: 20px; 13 | border: 1px solid transparent; 14 | border-radius: 4px; 15 | color: #31708f; 16 | background-color: #d9edf7; 17 | border-color: #bce8f1; 18 | } 19 | #login-box { 20 | width: 500px; 21 | padding: 20px; 22 | margin: 50px auto; 23 | background: #fff; 24 | -webkit-border-radius: 2px; 25 | -moz-border-radius: 2px; 26 | border: 1px solid #000; 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/resources/templates/admin.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security Basic - Form Based Authentication 6 | 7 | 8 |
9 |

10 |

11 |

12 | Welcome 13 | | Logout 14 |

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 7 |
8 |

Home Page

9 | Go to Administrator page 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | Spring Security Basic - Form Based Authentication 8 | 9 | 10 |
11 |

${title}

12 |

${message}

13 | 14 |

15 | Welcome 16 | : ${pageContext.request.userPrincipal.name} | Logout 18 |

19 |
20 |
21 | 22 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | 8 | 9 |
10 |

Home Page

11 | Go to Administrator page 12 |
13 | 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-auth-remember/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | " /> 9 | 10 | 11 |
12 |

Login Here

13 | 14 |
${error}
15 |
16 | 17 |
${msg}
18 |
19 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 |
User:
Password:
Remember Me:
39 | 41 |
42 |
43 | 44 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.6.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | compile('org.apache.tomcat.embed:tomcat-embed-jasper:9.0.22') 29 | compile('javax.servlet:jstl:1.2') 30 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-form-based-authentication/ 2 | 3 | For spring boot version 3 the java files are appended with _3. 4 | For spring boot version 3 the view files and css file are kept under src/main/resources folder. 5 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/authentication/SpringSecurityFormBasedAuthApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.authentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.boot.security.form.based.authentication") 7 | public class SpringSecurityFormBasedAuthApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormBasedAuthApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/authentication/SpringSecurityFormBasedAuthApp.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.authentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityFormBasedAuthApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormBasedAuthApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/authentication/config/PasswordEncryptorConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.authentication.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class PasswordEncryptorConfig { 10 | 11 | @Bean 12 | public PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/views/ 2 | spring.mvc.view.suffix=.jsp -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | .msg { 11 | padding: 15px; 12 | margin-bottom: 20px; 13 | border: 1px solid transparent; 14 | border-radius: 4px; 15 | color: #31708f; 16 | background-color: #d9edf7; 17 | border-color: #bce8f1; 18 | } 19 | #login-box { 20 | width: 500px; 21 | padding: 20px; 22 | margin: 50px auto; 23 | background: #fff; 24 | -webkit-border-radius: 2px; 25 | -moz-border-radius: 2px; 26 | border: 1px solid #000; 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/resources/templates/admin.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security Basic - Form Based Authentication 6 | 7 | 8 |
9 |

10 |

11 |

12 | Welcome 13 | | Logout 14 |

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 7 |
8 |

Home Page

9 | Go to Administrator page 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 35 | 36 | 37 |
38 |

Login Here

39 |
40 |
41 |
42 |
43 | 44 |
45 |
46 | 47 |
48 | 49 |
50 |
51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | Spring Security Basic - Form Based Authentication 8 | 9 | 10 |
11 |

${title}

12 |

${message}

13 | 14 |

15 | Welcome 16 | : ${pageContext.request.userPrincipal.name} | Logout 18 |

19 |
20 |
21 | 22 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | 8 | 9 |
10 |

Home Page

11 | Go to Administrator page 12 |
13 | 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-authentication/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | Spring Security Basic - Form Based Authentication 7 | " /> 9 | 10 | 11 |
12 |

Login Here

13 | 14 |
${error}
15 |
16 | 17 |
${msg}
18 |
19 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 |
User:
Password:
35 | 37 |
38 |
39 | 40 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.6.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | implementation("org.springframework:spring-jdbc:5.1.8.RELEASE") 29 | implementation('org.apache.tomcat.embed:tomcat-embed-jasper:9.0.22') 30 | implementation('javax.servlet:jstl:1.2') 31 | runtime("com.h2database:h2:1.4.196") 32 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/mysql_sql.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `user` ( 2 | `user_name` varchar(30) NOT NULL, 3 | `user_pass` varchar(255) NOT NULL, 4 | `enable` tinyint NOT NULL DEFAULT '1', 5 | PRIMARY KEY (`user_name`) 6 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 7 | 8 | CREATE TABLE IF NOT EXISTS `user_role` ( 9 | `user_name` varchar(30) NOT NULL, 10 | `user_role` varchar(15) NOT NULL, 11 | KEY `user_name` (`user_name`), 12 | CONSTRAINT `user_role_ibfk_1` FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 14 | 15 | INSERT INTO `user` (`user_name`, `user_pass`, `enable`) VALUES 16 | ('admin', '$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK', 1); 17 | 18 | 19 | INSERT INTO `user_role` (`user_name`, `user_role`) VALUES 20 | ('admin', 'ROLE_ADMIN'); 21 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-form-based-jdbc-authentication/ 2 | 3 | For spring boot 3 changes the files are generally appended with _3 and static files are kept under src/main/resources folder. 4 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/authentication/SpringSecurityFormBasedJdbcAuthApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.authentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.boot.security.form.based.jdbc.authentication") 7 | public class SpringSecurityFormBasedJdbcAuthApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormBasedJdbcAuthApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/authentication/SpringSecurityFormBasedJdbcAuthApp.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.authentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityFormBasedJdbcAuthApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityFormBasedJdbcAuthApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/authentication/config/DatabaseConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.authentication.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 9 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 10 | 11 | @Configuration 12 | public class DatabaseConfig { 13 | 14 | @Bean 15 | public DataSource dataSource() { 16 | EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 17 | EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) // .H2 or .DERBY, etc. 18 | .addScript("user.sql").addScript("user-role.sql").build(); 19 | return db; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/authentication/config/PasswordEncryptorConfig.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.authentication.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class PasswordEncryptorConfig { 10 | 11 | @Bean 12 | public PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/views/ 2 | spring.mvc.view.suffix=.jsp -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/application.properties_3: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | .msg { 11 | padding: 15px; 12 | margin-bottom: 20px; 13 | border: 1px solid transparent; 14 | border-radius: 4px; 15 | color: #31708f; 16 | background-color: #d9edf7; 17 | border-color: #bce8f1; 18 | } 19 | #login-box { 20 | width: 500px; 21 | padding: 20px; 22 | margin: 50px auto; 23 | background: #fff; 24 | -webkit-border-radius: 2px; 25 | -moz-border-radius: 2px; 26 | border: 1px solid #000; 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/templates/admin.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security Basic - Form Based Authentication 6 | 7 | 8 |
9 |

10 |

11 |

12 | Welcome 13 | | Logout 14 |

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 7 |
8 |

Home Page

9 | Go to Administrator page 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 35 | 36 | 37 |
38 |

Login Here

39 |
40 |
41 |
42 |
43 | 44 |
45 |
46 | 47 |
48 | 49 |
50 |
51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/user-role.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user_role` */ 2 | 3 | DROP TABLE IF EXISTS `user_role`; 4 | 5 | CREATE TABLE `user_role` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_role` varchar(15) NOT NULL, 8 | FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 9 | ); 10 | 11 | /*Data for the table `user_role` */ 12 | 13 | insert into `user_role`(`user_name`,`user_role`) values ('admin','ROLE_ADMIN'); -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/resources/user.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user` */ 2 | 3 | DROP TABLE IF EXISTS `user`; 4 | 5 | CREATE TABLE `user` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_pass` varchar(255) NOT NULL, 8 | `enable` tinyint(1) NOT NULL DEFAULT '1', 9 | PRIMARY KEY (`user_name`) 10 | ); 11 | 12 | /*Data for the table `user` */ 13 | 14 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('admin','$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK',1); -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | Spring Security Basic - Form Based JDBC Authentication 8 | 9 | 10 |
11 |

${title}

12 |

${message}

13 | 14 |

15 | Welcome 16 | : ${pageContext.request.userPrincipal.name} | Logout 18 |

19 |
20 |
21 | 22 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | Spring Security Basic - Form Based JDBC Authentication 7 | 8 | 9 |
10 |

Home Page

11 | Go to Administrator page 12 |
13 | 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-authentication/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | Spring Security Basic - Form Based JDBC Authentication 7 | " /> 9 | 10 | 11 |
12 |

Login Here

13 | 14 |
${error}
15 |
16 | 17 |
${msg}
18 |
19 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 |
User:
Password:
35 | 37 |
38 |
39 | 40 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.6.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | implementation("org.springframework:spring-jdbc:5.1.8.RELEASE") 29 | implementation('org.apache.tomcat.embed:tomcat-embed-jasper:9.0.22') 30 | implementation('javax.servlet:jstl:1.2') 31 | runtime("com.h2database:h2:1.4.196") 32 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-form-based-jdbc-authentication-using-userdetailsservice/ 2 | 3 | For spring boot 3 changes, the file generally appended with _3 and static resources are kept under src/main/resources folder. 4 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/config/DatabaseConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.jdbc.core.JdbcTemplate; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; 9 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 10 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 11 | 12 | @Configuration 13 | public class DatabaseConfig { 14 | 15 | @Bean 16 | public DataSource dataSource() { 17 | EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 18 | EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) // .H2 or .DERBY, etc. 19 | .addScript("user.sql").addScript("user-role.sql").build(); 20 | return db; 21 | } 22 | 23 | @Bean 24 | public JdbcTemplate jdbcTemplate() { 25 | return new JdbcTemplate(dataSource()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/config/EncoderConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class EncoderConfig { 10 | 11 | @Bean 12 | PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jdbc.core.JdbcTemplate; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.model.User; 8 | import com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.rowmapper.UserRowMapper; 9 | 10 | @Repository 11 | public class UserDao { 12 | 13 | @Autowired 14 | private JdbcTemplate jdbcTemplate; 15 | 16 | public User getUser(final String username) { 17 | return jdbcTemplate.queryForObject( 18 | "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name", 19 | new String[] { username }, new UserRowMapper()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/dao/UserDao.java_2.7.4: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jdbc.core.JdbcTemplate; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.model.User; 8 | import com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.rowmapper.UserRowMapper; 9 | 10 | @Repository 11 | public class UserDao { 12 | 13 | @Autowired 14 | private JdbcTemplate jdbcTemplate; 15 | 16 | public User getUser(final String username) { 17 | return jdbcTemplate.queryForObject( 18 | "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from `user` u, user_role ur where u.user_name = ? and u.user_name = ur.user_name", 19 | new UserRowMapper(), username); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/model/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.model; 2 | 3 | public class User { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public User() { 10 | } 11 | 12 | public User(String username, String password, String role) { 13 | this.username = username; 14 | this.password = password; 15 | this.role = role; 16 | } 17 | 18 | public String getUsername() { 19 | return username; 20 | } 21 | 22 | public void setUsername(String username) { 23 | this.username = username; 24 | } 25 | 26 | public String getPassword() { 27 | return password; 28 | } 29 | 30 | public void setPassword(String password) { 31 | this.password = password; 32 | } 33 | 34 | public String getRole() { 35 | return role; 36 | } 37 | 38 | public void setRole(String role) { 39 | this.role = role; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/java/com/roytuts/spring/boot/security/form/based/jdbc/userdetailsservice/auth/rowmapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.rowmapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.boot.security.form.based.jdbc.userdetailsservice.auth.model.User; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public User mapRow(ResultSet rs, int rowNum) throws SQLException { 14 | return new User(rs.getString("user_name"), rs.getString("user_pass"), rs.getString("user_role")); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/views/ 2 | spring.mvc.view.suffix=.jsp -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/application.properties_3: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | .msg { 11 | padding: 15px; 12 | margin-bottom: 20px; 13 | border: 1px solid transparent; 14 | border-radius: 4px; 15 | color: #31708f; 16 | background-color: #d9edf7; 17 | border-color: #bce8f1; 18 | } 19 | #login-box { 20 | width: 500px; 21 | padding: 20px; 22 | margin: 50px auto; 23 | background: #fff; 24 | -webkit-border-radius: 2px; 25 | -moz-border-radius: 2px; 26 | border: 1px solid #000; 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/templates/admin.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security Basic - Form Based Authentication 6 | 7 | 8 |
9 |

10 |

11 |

12 | Welcome 13 | | Logout 14 |

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Basic - Form Based Authentication 5 | 6 | 7 |
8 |

Home Page

9 | Go to Administrator page 10 |
11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/user-role.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user_role` */ 2 | 3 | DROP TABLE IF EXISTS `user_role`; 4 | 5 | CREATE TABLE `user_role` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_role` varchar(15) NOT NULL, 8 | FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 9 | ); 10 | 11 | /*Data for the table `user_role` */ 12 | 13 | insert into `user_role`(`user_name`,`user_role`) values ('admin','ROLE_ADMIN'); -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/user.sql: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user` */ 2 | 3 | DROP TABLE IF EXISTS `user`; 4 | 5 | CREATE TABLE `user` ( 6 | `user_name` varchar(30) NOT NULL, 7 | `user_pass` varchar(255) NOT NULL, 8 | `enable` tinyint(1) NOT NULL DEFAULT '1', 9 | PRIMARY KEY (`user_name`) 10 | ); 11 | 12 | /*Data for the table `user` */ 13 | 14 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('admin','$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK',1); -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/resources/user.sql_2.7.4: -------------------------------------------------------------------------------- 1 | /*Table structure for table `user` */ 2 | CREATE TABLE IF NOT EXISTS `user` ( 3 | `user_name` varchar(30) NOT NULL, 4 | `user_pass` varchar(255) NOT NULL, 5 | `enable` tinyint NOT NULL DEFAULT '1', 6 | PRIMARY KEY (`user_name`) 7 | ); 8 | /*Data for the table `user` */ 9 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('admin','$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK',1); 10 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | 5 | 6 | 7 | Spring Security Basic - Form Based JDBC Authentication 8 | 9 | 10 |
11 |

${title}

12 |

${message}

13 | 14 |

15 | Welcome 16 | : ${pageContext.request.userPrincipal.name} | Logout 18 |

19 |
20 |
21 | 22 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | 4 | 5 | 6 | Spring Security Basic - Form Based JDBC Authentication 7 | 8 | 9 |
10 |

Home Page

11 | Go to Administrator page 12 |
13 | 14 | -------------------------------------------------------------------------------- /spring-boot-security-form-based-jdbc-userdetailsservice-auth/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | Spring Security Basic - Form Based JDBC Authentication 7 | " /> 9 | 10 | 11 |
12 |

Login Here

13 | 14 |
${error}
15 |
16 | 17 |
${msg}
18 |
19 |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 |
User:
Password:
35 | 37 |
38 |
39 | 40 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/angular-spring-boot-security-jwt-authentication-and-authorization/ -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/SpringSecurityJwtApiApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityJwtApiApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityJwtApiApp.class, args); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/config/ApiAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.config; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.security.core.AuthenticationException; 10 | import org.springframework.security.web.AuthenticationEntryPoint; 11 | import org.springframework.stereotype.Component; 12 | 13 | @Component 14 | public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint { 15 | 16 | @Override 17 | public void commence(HttpServletRequest request, HttpServletResponse response, 18 | AuthenticationException authException) throws IOException, ServletException { 19 | response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.OneToMany; 13 | import javax.persistence.Table; 14 | 15 | @Table 16 | @Entity 17 | public class User { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | private int id; 22 | 23 | @Column 24 | private String userName; 25 | 26 | @Column 27 | private String userPass; 28 | 29 | @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) 30 | private Set userRoles = new HashSet<>(); 31 | 32 | public int getId() { 33 | return id; 34 | } 35 | 36 | public void setId(int id) { 37 | this.id = id; 38 | } 39 | 40 | public String getUserName() { 41 | return userName; 42 | } 43 | 44 | public void setUserName(String userName) { 45 | this.userName = userName; 46 | } 47 | 48 | public String getUserPass() { 49 | return userPass; 50 | } 51 | 52 | public void setUserPass(String userPass) { 53 | this.userPass = userPass; 54 | } 55 | 56 | public Set getUserRoles() { 57 | return userRoles; 58 | } 59 | 60 | public void setUserRoles(Set userRoles) { 61 | this.userRoles = userRoles; 62 | 63 | for (UserRole r : userRoles) { 64 | r.setUser(this); 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.FetchType; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.ManyToOne; 11 | import javax.persistence.Table; 12 | 13 | @Table 14 | @Entity 15 | public class UserRole { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private int id; 20 | 21 | @Column 22 | private String role; 23 | 24 | @JoinColumn(name = "user_id") 25 | @ManyToOne(fetch = FetchType.LAZY, optional = false) 26 | private User user; 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | 36 | public String getRole() { 37 | return role; 38 | } 39 | 40 | public void setRole(String role) { 41 | this.role = role; 42 | } 43 | 44 | public User getUser() { 45 | return user; 46 | } 47 | 48 | public void setUser(User user) { 49 | this.user = user; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/exception/DisabledUserException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.exception; 2 | 3 | public class DisabledUserException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public DisabledUserException(String msg) { 8 | super(msg); 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/exception/InvalidUserCredentialsException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.exception; 2 | 3 | public class InvalidUserCredentialsException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public InvalidUserCredentialsException(String msg) { 8 | super(msg); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/exception/JwtTokenMalformedException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.exception; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | 5 | public class JwtTokenMalformedException extends AuthenticationException { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | public JwtTokenMalformedException(String msg) { 10 | super(msg); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/exception/JwtTokenMissingException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.exception; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | 5 | public class JwtTokenMissingException extends AuthenticationException { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | public JwtTokenMissingException(String msg) { 10 | super(msg); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.roytuts.spring.boot.security.jwt.auth.api.entity.User; 8 | 9 | public interface UserRepository extends JpaRepository { 10 | 11 | Optional findByUserName(String userName); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/vo/Request.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.vo; 2 | 3 | import java.util.List; 4 | 5 | public class Request { 6 | 7 | private String userName; 8 | private String userPwd; 9 | private List roles; 10 | 11 | public String getUserName() { 12 | return userName; 13 | } 14 | 15 | public void setUserName(String userName) { 16 | this.userName = userName; 17 | } 18 | 19 | public String getUserPwd() { 20 | return userPwd; 21 | } 22 | 23 | public void setUserPwd(String userPwd) { 24 | this.userPwd = userPwd; 25 | } 26 | 27 | public List getRoles() { 28 | return roles; 29 | } 30 | 31 | public void setRoles(List roles) { 32 | this.roles = roles; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/java/com/roytuts/spring/boot/security/jwt/auth/api/vo/Response.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.security.jwt.auth.api.vo; 2 | 3 | import java.util.List; 4 | 5 | public class Response { 6 | 7 | private String token; 8 | private List roles; 9 | 10 | public String getToken() { 11 | return token; 12 | } 13 | 14 | public List getRoles() { 15 | return roles; 16 | } 17 | 18 | public void setRoles(List roles) { 19 | this.roles = roles; 20 | } 21 | 22 | public void setToken(String token) { 23 | this.token = token; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-security-jwt-auth-api/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #secret key - should be encrypted 8 | jwt.secret=secretkey 9 | #3 minutes validity 10 | jwt.token.validity=180000 11 | 12 | #ignore null fields in json 13 | spring.jackson.default-property-inclusion=NON_NULL 14 | 15 | #SQL related 16 | spring.jpa.show-sql = true 17 | spring.jpa.properties.hibernate.format_sql=true 18 | logging.level.org.hibernate.type.descriptor.sql=trace 19 | 20 | spring.jpa.hibernate.ddl-auto = none 21 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.1.RELEASE' //to 2.4.4 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version "${springBootVersion}" 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}" 29 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 30 | implementation('com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3') 31 | runtime("mysql:mysql-connector-java:8.0.17") //to 8.0.22 32 | 33 | //required for jdk 9 or above 34 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 35 | } -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-enableencryptableproperties-with-jasypt/ 2 | 3 | For spring boot version 3 you don't need to create the configuration class for database connectivity. The database configuration in application.properties_3 file will be sufficient. 4 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/src/main/java/com/roytuts/spring/jasypt/enableencryptableproperties/JasyptEnableEncryptablePropertiesApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jasypt.enableencryptableproperties; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import com.roytuts.spring.jasypt.enableencryptableproperties.service.UserService; 9 | 10 | @SpringBootApplication 11 | public class JasyptEnableEncryptablePropertiesApp implements CommandLineRunner { 12 | 13 | @Autowired 14 | private UserService service; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(JasyptEnableEncryptablePropertiesApp.class, args); 18 | } 19 | 20 | @Override 21 | public void run(String... args) throws Exception { 22 | System.out.println(service.getUserList()); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/src/main/java/com/roytuts/spring/jasypt/enableencryptableproperties/repository/UserJpaRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jasypt.enableencryptableproperties.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.jasypt.enableencryptableproperties.model.User; 6 | 7 | public interface UserJpaRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/src/main/java/com/roytuts/spring/jasypt/enableencryptableproperties/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.jasypt.enableencryptableproperties.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.jasypt.enableencryptableproperties.model.User; 9 | import com.roytuts.spring.jasypt.enableencryptableproperties.repository.UserJpaRepository; 10 | 11 | @Service 12 | public class UserService { 13 | 14 | @Autowired 15 | private UserJpaRepository repository; 16 | 17 | public List getUserList() { 18 | return repository.findAll(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/roytuts 2 | spring.datasource.username=root 3 | spring.datasource.password=ENC(ZbkVUeL0Z7/ZbiGV9dh6mA==) 4 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5 | jasypt.encryptor.algorithm=PBEWithMD5AndDES 6 | jasypt.encryptor.password=test 7 | jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator 8 | 9 | #disable schema generation from Hibernate 10 | spring.jpa.hibernate.ddl-auto=none -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/src/main/resources/application.properties_3: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=ENC(ZbkVUeL0Z7/ZbiGV9dh6mA==) 6 | jasypt.encryptor.algorithm=PBEWithMD5AndDES 7 | jasypt.encryptor.password=test 8 | jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator 9 | -------------------------------------------------------------------------------- /spring-jasypt-enableencryptableproperties/user.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `user` ( 2 | `id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, 5 | `phone` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `address` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL, 7 | PRIMARY KEY (`id`) 8 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 9 | 10 | INSERT INTO `user` (`id`, `name`, `email`, `phone`, `address`) VALUES 11 | (1, 'Soumitra Roy', 'sroy@gmail.com', 2147483647, 'Earth'), 12 | (2, 'Rahul Kumar', 'rahul@gmail.com', 34256780, 'Mars'); -------------------------------------------------------------------------------- /spring-oauth2-sso-aws-cognito/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/spring-boot-oauth2-sso-example-with-aws-cognito/ 2 | -------------------------------------------------------------------------------- /spring-oauth2-sso-aws-cognito/src/main/java/com/roytuts/spring/oauth2/sso/OAuth2SsoSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.oauth2.sso; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 6 | 7 | @Configuration 8 | public class OAuth2SsoSecurityConfig extends WebSecurityConfigurerAdapter { 9 | 10 | @Override 11 | protected void configure(HttpSecurity http) throws Exception { 12 | http.csrf().and() 13 | .authorizeRequests(authorize -> authorize.mvcMatchers("/").permitAll().anyRequest().authenticated()) 14 | .oauth2Login().and().logout().logoutUrl("/logout").logoutSuccessUrl("/"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-oauth2-sso-aws-cognito/src/main/java/com/roytuts/spring/oauth2/sso/SpringOAuth2SsoApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.oauth2.sso; 2 | 3 | import java.util.stream.Collectors; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.security.core.Authentication; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.ui.Model; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | 12 | @Controller 13 | @SpringBootApplication 14 | public class SpringOAuth2SsoApp { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SpringOAuth2SsoApp.class, args); 18 | } 19 | 20 | @GetMapping("/") 21 | public String home(Model model, Authentication authentication) { 22 | if (authentication != null && authentication.isAuthenticated()) { 23 | model.addAttribute("name", authentication.getName()); 24 | model.addAttribute("principal", authentication.getPrincipal()); 25 | model.addAttribute("authorities", authentication.getAuthorities().stream().map(a -> a.getAuthority()) 26 | .collect(Collectors.joining(","))); 27 | } 28 | 29 | model.addAttribute("message", "Spring Boot OAuth2 SSO with AWS Cognito"); 30 | 31 | return "index"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-oauth2-sso-aws-cognito/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8089 3 | 4 | spring: 5 | security: 6 | oauth2: 7 | client: 8 | registration: 9 | cognito: 10 | clientId: 11 | clientSecret: 12 | scope: openid, email 13 | authorization-grant-type: authorization_code 14 | redirect-uri: "{baseUrl}/login/oauth2/code/cognito" 15 | clientName: 16 | provider: 17 | cognito: 18 | user-info-uri: 19 | jwk-set-uri: 20 | token-uri: 21 | authorization-uri: 22 | userNameAttribute: sub -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/preauthorize-haspermission-security-using-spring-boot-data-jpa/ 2 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan(basePackages = "com.roytuts.spring.preauthorize.haspermission.data.jpa.entity") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.preauthorize.haspermission.data.jpa.repository") 11 | public class App { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(App.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/config/CustomPermissionEvaluator.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.config; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.springframework.security.access.PermissionEvaluator; 6 | import org.springframework.security.core.Authentication; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Component 10 | public class CustomPermissionEvaluator implements PermissionEvaluator { 11 | 12 | @Override 13 | public boolean hasPermission(Authentication authentication, Object accessType, Object permission) { 14 | if (authentication != null && accessType instanceof String) { 15 | if ("hasAccess".equalsIgnoreCase(String.valueOf(accessType))) { 16 | boolean hasAccess = validateAccess(String.valueOf(permission)); 17 | return hasAccess; 18 | } 19 | return false; 20 | } 21 | return false; 22 | } 23 | 24 | private boolean validateAccess(String permission) { 25 | // ideally should be checked with user role, permission in database 26 | if ("READ".equalsIgnoreCase(permission)) { 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | @Override 33 | public boolean hasPermission(Authentication authentication, Serializable serializable, String targetType, 34 | Object permission) { 35 | return false; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/config/EncoderConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class EncoderConfig { 10 | 11 | @Bean 12 | PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/config/PermissionConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; 7 | import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; 8 | 9 | @Configuration 10 | public class PermissionConfig { 11 | 12 | @Autowired 13 | private CustomPermissionEvaluator permissionEvaluator; 14 | 15 | @Bean 16 | public MethodSecurityExpressionHandler methodSecurityExpressionHandler() { 17 | DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler(); 18 | handler.setPermissionEvaluator(permissionEvaluator); 19 | return handler; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | import org.springframework.security.crypto.password.PasswordEncoder; 10 | 11 | import com.roytuts.spring.preauthorize.haspermission.data.jpa.service.UserAuthService; 12 | 13 | @Configuration 14 | @EnableWebSecurity 15 | @EnableGlobalMethodSecurity(prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Autowired 19 | private PasswordEncoder passwordEncoder; 20 | 21 | @Autowired 22 | private UserAuthService userAuthService; 23 | 24 | @Autowired 25 | public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { 26 | auth.userDetailsService(userAuthService).passwordEncoder(passwordEncoder); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/config/SecurityConfig.java_3_2_5: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7 | import org.springframework.security.crypto.password.PasswordEncoder; 8 | 9 | import com.roytuts.spring.preauthorize.haspermission.data.jpa.service.UserAuthService; 10 | 11 | @Configuration 12 | @EnableWebSecurity 13 | public class SecurityConfig { 14 | 15 | @Autowired 16 | private PasswordEncoder passwordEncoder; 17 | 18 | @Autowired 19 | private UserAuthService userAuthService; 20 | 21 | @Autowired 22 | public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { 23 | auth.userDetailsService(userAuthService).passwordEncoder(passwordEncoder); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.JoinColumn; 9 | import javax.persistence.ManyToOne; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "user_role") 14 | public class UserRole { 15 | 16 | @Id 17 | @Column(name = "role_id") 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Integer roleId; 20 | 21 | @Column(name = "user_role") 22 | private String userRole; 23 | 24 | @ManyToOne 25 | @JoinColumn(name = "user_id") 26 | private User user; 27 | 28 | public Integer getRoleId() { 29 | return roleId; 30 | } 31 | 32 | public void setRoleId(Integer roleId) { 33 | this.roleId = roleId; 34 | } 35 | 36 | public String getUserRole() { 37 | return userRole; 38 | } 39 | 40 | public void setUserRole(String userRole) { 41 | this.userRole = userRole; 42 | } 43 | 44 | public User getUser() { 45 | return user; 46 | } 47 | 48 | public void setUser(User user) { 49 | this.user = user; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.preauthorize.haspermission.data.jpa.entity.User; 6 | 7 | public interface UserRepository extends JpaRepository { 8 | 9 | User findByUserName(final String userName); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/repository/UserRoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.preauthorize.haspermission.data.jpa.entity.UserRole; 6 | 7 | public interface UserRoleRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/java/com/roytuts/spring/preauthorize/haspermission/data/jpa/rest/controller/AppRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.haspermission.data.jpa.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.security.access.prepost.PreAuthorize; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class AppRestController { 11 | 12 | @GetMapping("/user") 13 | @PreAuthorize("hasRole('USER')") 14 | public ResponseEntity user() { 15 | return new ResponseEntity("You have USER role.", HttpStatus.OK); 16 | } 17 | 18 | @GetMapping("/admin") 19 | @PreAuthorize("hasRole('ADMIN') and hasPermission('hasAccess','READ')") 20 | public ResponseEntity admin() { 21 | return new ResponseEntity("You have ADMIN role with READ access.", HttpStatus.OK); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #SQL related 8 | spring.jpa.show-sql = true 9 | spring.jpa.properties.hibernate.format_sql=true 10 | 11 | spring.jpa.hibernate.ddl-auto = none 12 | -------------------------------------------------------------------------------- /spring-preauthorize-has-permission-data-jpa/user.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `user` ( 2 | `user_id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 3 | `user_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 4 | `user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 5 | `enable` tinyint COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '1', 6 | PRIMARY KEY (`user_id`), 7 | UNIQUE KEY `user_unique_key` (`user_name`) 8 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 9 | 10 | CREATE TABLE IF NOT EXISTS `user_role` ( 11 | `role_id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL AUTO_INCREMENT, 12 | `user_id` int unsigned COLLATE utf8mb4_unicode_ci NOT NULL, 13 | `user_role` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL, 14 | PRIMARY KEY (`role_id`), 15 | UNIQUE KEY `user_unique_key` (`user_id`, `user_role`), 16 | CONSTRAINT `user_role_fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) 17 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 18 | 19 | INSERT INTO `user` (`user_id`, `user_name`, `user_pass`, `enable`) VALUES 20 | (1, 'admin', '$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK', 1), 21 | (2, 'user', '$2a$10$9Xn39aPf4LhDpRGNWvDFqu.T5ZPHbyh8iNQDSb4aNSnLqE2u2efIu', 1); 22 | 23 | 24 | INSERT INTO `user_role` (`role_id`, `user_id`, `user_role`) VALUES 25 | (1, 2, 'ROLE_USER'), 26 | (2, 1, 'ROLE_USER'), 27 | (3, 1, 'ROLE_ADMIN'); 28 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/preauthorize-hasrole-security-using-spring-boot-data-jpa/ 2 | 3 | Spring boot 3 related changes are kept into files which are appended with _3. 4 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan(basePackages = "com.roytuts.spring.preauthorize.hasrole.data.jpa.entity") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.preauthorize.hasrole.data.jpa.repository") 11 | public class App { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(App.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/App.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/config/EncoderConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class EncoderConfig { 10 | 11 | @Bean 12 | PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | import org.springframework.security.crypto.password.PasswordEncoder; 10 | 11 | import com.roytuts.spring.preauthorize.hasrole.data.jpa.service.UserAuthService; 12 | 13 | @Configuration 14 | @EnableWebSecurity 15 | @EnableGlobalMethodSecurity(prePostEnabled = true) 16 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 17 | 18 | @Autowired 19 | private PasswordEncoder passwordEncoder; 20 | 21 | @Autowired 22 | private UserAuthService userAuthService; 23 | 24 | @Autowired 25 | public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { 26 | auth.userDetailsService(userAuthService).passwordEncoder(passwordEncoder); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/config/SecurityConfig.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; 7 | import org.springframework.security.crypto.password.PasswordEncoder; 8 | 9 | import com.roytuts.spring.preauthorize.hasrole.data.jpa.service.UserAuthService; 10 | 11 | @Configuration 12 | @EnableMethodSecurity 13 | public class SecurityConfig { 14 | 15 | @Autowired 16 | private PasswordEncoder passwordEncoder; 17 | 18 | @Autowired 19 | private UserAuthService userAuthService; 20 | 21 | @Autowired 22 | public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { 23 | auth.userDetailsService(userAuthService).passwordEncoder(passwordEncoder); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.JoinColumn; 9 | import javax.persistence.ManyToOne; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "user_role") 14 | public class UserRole { 15 | 16 | @Id 17 | @Column(name = "role_id") 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Integer roleId; 20 | 21 | @Column(name = "user_role") 22 | private String userRole; 23 | 24 | @ManyToOne 25 | @JoinColumn(name = "user_id") 26 | private User user; 27 | 28 | public Integer getRoleId() { 29 | return roleId; 30 | } 31 | 32 | public void setRoleId(Integer roleId) { 33 | this.roleId = roleId; 34 | } 35 | 36 | public String getUserRole() { 37 | return userRole; 38 | } 39 | 40 | public void setUserRole(String userRole) { 41 | this.userRole = userRole; 42 | } 43 | 44 | public User getUser() { 45 | return user; 46 | } 47 | 48 | public void setUser(User user) { 49 | this.user = user; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/entity/UserRole.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.entity; 2 | 3 | import jakarta.persistence.Column; 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.GeneratedValue; 6 | import jakarta.persistence.GenerationType; 7 | import jakarta.persistence.Id; 8 | import jakarta.persistence.JoinColumn; 9 | import jakarta.persistence.ManyToOne; 10 | import jakarta.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "user_role") 14 | public class UserRole { 15 | 16 | @Id 17 | @Column(name = "role_id") 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Integer roleId; 20 | 21 | @Column(name = "user_role") 22 | private String userRole; 23 | 24 | @ManyToOne 25 | @JoinColumn(name = "user_id") 26 | private User user; 27 | 28 | public Integer getRoleId() { 29 | return roleId; 30 | } 31 | 32 | public void setRoleId(Integer roleId) { 33 | this.roleId = roleId; 34 | } 35 | 36 | public String getUserRole() { 37 | return userRole; 38 | } 39 | 40 | public void setUserRole(String userRole) { 41 | this.userRole = userRole; 42 | } 43 | 44 | public User getUser() { 45 | return user; 46 | } 47 | 48 | public void setUser(User user) { 49 | this.user = user; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.preauthorize.hasrole.data.jpa.entity.User; 6 | 7 | public interface UserRepository extends JpaRepository { 8 | 9 | User findByUserName(final String userName); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/repository/UserRoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.preauthorize.hasrole.data.jpa.entity.UserRole; 6 | 7 | public interface UserRoleRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/java/com/roytuts/spring/preauthorize/hasrole/data/jpa/rest/controller/AppRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.preauthorize.hasrole.data.jpa.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.security.access.prepost.PreAuthorize; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class AppRestController { 12 | 13 | @GetMapping("/user") 14 | @PreAuthorize("hasRole('USER')") 15 | public ResponseEntity defaultPage(Model model) { 16 | return new ResponseEntity("You have USER role.", HttpStatus.OK); 17 | } 18 | 19 | @GetMapping("/admin") 20 | @PreAuthorize("hasRole('ADMIN')") 21 | public ResponseEntity getAllBlogs(Model model) { 22 | return new ResponseEntity("You have ADMIN role.", HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-preauthorize-hasrole-data-jpa/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #SQL related 8 | spring.jpa.show-sql = true 9 | spring.jpa.properties.hibernate.format_sql=true 10 | 11 | spring.jpa.hibernate.ddl-auto = none 12 | -------------------------------------------------------------------------------- /spring-security-annotations/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-form-based-authentication-annotations/ 2 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/java/com/roytuts/spring/security/annotations/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.annotations.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebSecurityConfig.class, WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/java/com/roytuts/spring/security/annotations/config/SecurityWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.annotations.config; 2 | 3 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 4 | 5 | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | page.title=Spring Security Basic (Annotations) 2 | page.home.heading=Home Page 3 | page.login.heading=Login Here 4 | page.admin.heading=Administrator Control Panel 5 | page.admin.message=This page demonstrates how to use Spring security. 6 | page.goto.admin=Go to Administrator page 7 | login.failure.reason=Invalid credentials 8 | welcome.msg=Welcome 9 | logout.text=Logout 10 | logout.msg.success=You have been successfully logged out. 11 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | 8 | <fmt:bundle basename="messages"> 9 | <fmt:message key="page.title" /> 10 | </fmt:bundle> 11 | 12 | 13 |
14 |

${title}

15 |

${message}

16 | 17 |

18 | 19 | 20 | 21 | : ${pageContext.request.userPrincipal.name} | 23 | 24 | 25 |

26 |
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 | 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | 11 | 12 |
13 |

14 | 15 | 16 | 17 |

18 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-security-annotations/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | " /> 12 | 13 | 14 |
15 |

16 | 17 | 18 | 19 |

20 | 21 |
${error}
22 |
23 | 24 |
${msg}
25 |
26 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 41 |
User:
Password:
42 | 44 |
45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/spring-security-pre-authentication-with-spring-data-jpa/ -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/PreAuthSecurityApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class PreAuthSecurityApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(PreAuthSecurityApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | 6 | @Entity 7 | public class User { 8 | 9 | @Id 10 | private String userName; 11 | private String userPass; 12 | private boolean enable; 13 | 14 | public String getUserName() { 15 | return userName; 16 | } 17 | 18 | public void setUserName(String userName) { 19 | this.userName = userName; 20 | } 21 | 22 | public String getUserPass() { 23 | return userPass; 24 | } 25 | 26 | public void setUserPass(String userPass) { 27 | this.userPass = userPass; 28 | } 29 | 30 | public boolean isEnable() { 31 | return enable; 32 | } 33 | 34 | public void setEnable(boolean enable) { 35 | this.enable = enable; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/entity/UserRole.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | 6 | @Entity 7 | public class UserRole { 8 | 9 | @Id 10 | private String userName; 11 | private String userRole; 12 | 13 | public String getUserName() { 14 | return userName; 15 | } 16 | 17 | public void setUserName(String userName) { 18 | this.userName = userName; 19 | } 20 | 21 | public String getUserRole() { 22 | return userRole; 23 | } 24 | 25 | public void setUserRole(String userRole) { 26 | this.userRole = userRole; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.roytuts.spring.security.data.jpa.preauthentication.entity.User; 8 | 9 | public interface UserRepository extends JpaRepository { 10 | 11 | Optional findByUserName(String userName); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/repository/UserRoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.roytuts.spring.security.data.jpa.preauthentication.entity.UserRole; 8 | 9 | public interface UserRoleRepository extends JpaRepository { 10 | 11 | Optional findByUserName(String userName); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/java/com/roytuts/spring/security/data/jpa/preauthentication/vo/BlogVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.data.jpa.preauthentication.vo; 2 | 3 | import java.util.Date; 4 | 5 | import com.fasterxml.jackson.annotation.JsonFormat; 6 | 7 | public class BlogVo { 8 | 9 | private String title; 10 | private String author; 11 | 12 | @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z") 13 | private Date date; 14 | private String text; 15 | 16 | public String getTitle() { 17 | return title; 18 | } 19 | 20 | public void setTitle(String title) { 21 | this.title = title; 22 | } 23 | 24 | public String getAuthor() { 25 | return author; 26 | } 27 | 28 | public void setAuthor(String author) { 29 | this.author = author; 30 | } 31 | 32 | public Date getDate() { 33 | return date; 34 | } 35 | 36 | public void setDate(Date date) { 37 | this.date = date; 38 | } 39 | 40 | public String getText() { 41 | return text; 42 | } 43 | 44 | public void setText(String text) { 45 | this.text = text; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:roytuts 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | 7 | spring.jpa.show-sql = true 8 | 9 | spring.h2.console.enabled = true 10 | spring.h2.console.path=/h2console/ 11 | 12 | spring.jpa.hibernate.ddl-auto = none 13 | 14 | spring.jpa.defer-datasource-initialization=true -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('roy','ae685575101ee7165c90a8f2c30c6e60cdd9e482',1); 2 | insert into `user_role`(`user_name`,`user_role`) values ('roy','ROLE_ADMIN'); -------------------------------------------------------------------------------- /spring-security-data-jpa-pre-authentication/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `user`; 2 | CREATE TABLE `user` ( 3 | `user_name` varchar(30) NOT NULL, 4 | `user_pass` varchar(255) NOT NULL, 5 | `enable` tinyint(1) NOT NULL DEFAULT '1', 6 | PRIMARY KEY (`user_name`) 7 | ); 8 | DROP TABLE IF EXISTS `user_role`; 9 | CREATE TABLE `user_role` ( 10 | `user_name` varchar(30) NOT NULL, 11 | `user_role` varchar(15) NOT NULL, 12 | FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 13 | ); -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-form-based-login-remember-me/ 2 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/java/com/roytuts/spring/security/jdbc/authentication/formbased/rememberme/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebSecurityConfig.class, WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/java/com/roytuts/spring/security/jdbc/authentication/formbased/rememberme/config/SecurityWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.config; 2 | 3 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 4 | 5 | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/java/com/roytuts/spring/security/jdbc/authentication/formbased/rememberme/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.dao.EmptyResultDataAccessException; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.model.UserDetails; 10 | import com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.rowmapper.UserRowMapper; 11 | 12 | @Repository 13 | @Transactional 14 | public class UserDao { 15 | 16 | @Autowired 17 | private JdbcTemplate jdbcTemplate; 18 | 19 | public UserDetails getUser(String username) { 20 | try { 21 | final String sql = "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name"; 22 | 23 | UserDetails userDetails = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username); 24 | 25 | return userDetails; 26 | } catch (EmptyResultDataAccessException ex) { 27 | return null;// should have proper handling of Exception 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/java/com/roytuts/spring/security/jdbc/authentication/formbased/rememberme/model/UserDetails.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.model; 2 | 3 | public class UserDetails { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public String getUsername() { 10 | return username; 11 | } 12 | 13 | public void setUsername(String username) { 14 | this.username = username; 15 | } 16 | 17 | public String getPassword() { 18 | return password; 19 | } 20 | 21 | public void setPassword(String password) { 22 | this.password = password; 23 | } 24 | 25 | public String getRole() { 26 | return role; 27 | } 28 | 29 | public void setRole(String role) { 30 | this.role = role; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/java/com/roytuts/spring/security/jdbc/authentication/formbased/rememberme/rowmapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.rowmapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.security.jdbc.authentication.formbased.rememberme.model.UserDetails; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public UserDetails mapRow(ResultSet rs, int row) throws SQLException { 14 | UserDetails userDetails = new UserDetails(); 15 | userDetails.setUsername(rs.getString("user_name")); 16 | userDetails.setPassword(rs.getString("user_pass")); 17 | userDetails.setRole(rs.getString("user_role")); 18 | 19 | return userDetails; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | page.title=Spring Security JDBC Authentication 2 | page.home.heading=Home Page 3 | page.login.heading=Login Here 4 | page.admin.heading=Administrator Control Panel 5 | page.admin.message=This page demonstrates how to use Spring security. 6 | page.goto.admin=Go to Administrator page 7 | login.failure.reason=Invalid credentials 8 | welcome.msg=Welcome 9 | logout.text=Logout 10 | logout.msg.success=You have been successfully logged out. 11 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | 8 | <fmt:bundle basename="messages"> 9 | <fmt:message key="page.title" /> 10 | </fmt:bundle> 11 | 12 | 13 |
14 |

${title}

15 |

${message}

16 | 17 |

18 | 19 | 20 | 21 | : ${pageContext.request.userPrincipal.name} | 23 | 24 | 25 |

26 |
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 | 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | 11 | 12 |
13 |

14 | 15 | 16 | 17 |

18 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-form-based-remember-me/user.sql: -------------------------------------------------------------------------------- 1 | USE `roytuts`; 2 | /*Table structure for table `user` */ 3 | DROP TABLE IF EXISTS `user`; 4 | CREATE TABLE `user` ( 5 | `user_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 7 | `enable` tinyint NOT NULL DEFAULT '1', 8 | PRIMARY KEY (`user_name`) 9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 | /*Data for the table `user` */ 11 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('roy','$2a$12$PN0MjtyNGWW.AjWdIuYKxe4.4Grjs4K7oanuAnt/WSDSvFlUc3eQi',1); 12 | 13 | /*Table structure for table `user_role` */ 14 | DROP TABLE IF EXISTS `user_role`; 15 | CREATE TABLE `user_role` ( 16 | `user_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 17 | `user_role` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL, 18 | KEY `fk_user` (`user_name`), 19 | CONSTRAINT `fk_user` FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 20 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 21 | /*Data for the table `user_role` */ 22 | insert into `user_role`(`user_name`,`user_role`) values ('roy','ROLE_ADMIN'); 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-remember-me-persistent-token-approach/ 2 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/com/roytuts/spring/security/jdbc/authentication/persistent/rememberme/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebSecurityConfig.class, WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/com/roytuts/spring/security/jdbc/authentication/persistent/rememberme/config/SecurityWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.config; 2 | 3 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 4 | 5 | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/com/roytuts/spring/security/jdbc/authentication/persistent/rememberme/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.dao.EmptyResultDataAccessException; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.model.UserDetails; 10 | import com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.rowmapper.UserRowMapper; 11 | 12 | @Repository 13 | @Transactional 14 | public class UserDao { 15 | 16 | @Autowired 17 | private JdbcTemplate jdbcTemplate; 18 | 19 | public UserDetails getUser(String username) { 20 | try { 21 | final String sql = "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name"; 22 | 23 | UserDetails userDetails = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username); 24 | 25 | return userDetails; 26 | } catch (EmptyResultDataAccessException ex) { 27 | return null;// should have proper handling of Exception 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/com/roytuts/spring/security/jdbc/authentication/persistent/rememberme/model/UserDetails.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.model; 2 | 3 | public class UserDetails { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public String getUsername() { 10 | return username; 11 | } 12 | 13 | public void setUsername(String username) { 14 | this.username = username; 15 | } 16 | 17 | public String getPassword() { 18 | return password; 19 | } 20 | 21 | public void setPassword(String password) { 22 | this.password = password; 23 | } 24 | 25 | public String getRole() { 26 | return role; 27 | } 28 | 29 | public void setRole(String role) { 30 | this.role = role; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/com/roytuts/spring/security/jdbc/authentication/persistent/rememberme/rowmapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.rowmapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.security.jdbc.authentication.persistent.rememberme.model.UserDetails; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public UserDetails mapRow(ResultSet rs, int row) throws SQLException { 14 | UserDetails userDetails = new UserDetails(); 15 | userDetails.setUsername(rs.getString("user_name")); 16 | userDetails.setPassword(rs.getString("user_pass")); 17 | userDetails.setRole(rs.getString("user_role")); 18 | 19 | return userDetails; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | page.title=Spring Security JDBC Authentication 2 | page.home.heading=Home Page 3 | page.login.heading=Login Here 4 | page.admin.heading=Administrator Control Panel 5 | page.admin.message=This page demonstrates how to use Spring security. 6 | page.goto.admin=Go to Administrator page 7 | login.failure.reason=Invalid credentials 8 | welcome.msg=Welcome 9 | logout.text=Logout 10 | logout.msg.success=You have been successfully logged out. 11 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | 8 | <fmt:bundle basename="messages"> 9 | <fmt:message key="page.title" /> 10 | </fmt:bundle> 11 | 12 | 13 |
14 |

${title}

15 |

${message}

16 | 17 |

18 | 19 | 20 | 21 | : ${pageContext.request.userPrincipal.name} | 23 | 24 | 25 |

26 |
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-persistent-token-remember-me/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 | 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | 11 | 12 |
13 |

14 | 15 | 16 | 17 |

18 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-jdbc-authentication-using-userdetailsservice/ 2 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/java/com/roytuts/spring/security/jdbc/authentication/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebSecurityConfig.class, WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/java/com/roytuts/spring/security/jdbc/authentication/config/SecurityWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.config; 2 | 3 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 4 | 5 | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/java/com/roytuts/spring/security/jdbc/authentication/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.dao.EmptyResultDataAccessException; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.roytuts.spring.security.jdbc.authentication.model.UserDetails; 10 | import com.roytuts.spring.security.jdbc.authentication.rowmapper.UserRowMapper; 11 | 12 | @Repository 13 | @Transactional 14 | public class UserDao { 15 | 16 | @Autowired 17 | private JdbcTemplate jdbcTemplate; 18 | 19 | public UserDetails getUser(String username) { 20 | try { 21 | final String sql = "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name"; 22 | 23 | UserDetails userDetails = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username); 24 | 25 | return userDetails; 26 | } catch (EmptyResultDataAccessException ex) { 27 | return null;// should have proper handling of Exception 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/java/com/roytuts/spring/security/jdbc/authentication/model/UserDetails.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.model; 2 | 3 | public class UserDetails { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public String getUsername() { 10 | return username; 11 | } 12 | 13 | public void setUsername(String username) { 14 | this.username = username; 15 | } 16 | 17 | public String getPassword() { 18 | return password; 19 | } 20 | 21 | public void setPassword(String password) { 22 | this.password = password; 23 | } 24 | 25 | public String getRole() { 26 | return role; 27 | } 28 | 29 | public void setRole(String role) { 30 | this.role = role; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/java/com/roytuts/spring/security/jdbc/authentication/rowmapper/UserRowMapper.java.java: -------------------------------------------------------------------------------- 1 | public class UserRowMapper implements RowMapper { 2 | 3 | @Override 4 | public UserDetails mapRow(ResultSet rs, int row) throws SQLException { 5 | UserDetails userDetails = new UserDetails(); 6 | userDetails.setUsername(rs.getString("user_name")); 7 | userDetails.setPassword(rs.getString("user_pass")); 8 | userDetails.setRole(rs.getString("user_role")); 9 | 10 | return userDetails; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | page.title=Spring Security JDBC Authentication 2 | page.home.heading=Home Page 3 | page.login.heading=Login Here 4 | page.admin.heading=Administrator Control Panel 5 | page.admin.message=This page demonstrates how to use Spring security. 6 | page.goto.admin=Go to Administrator page 7 | login.failure.reason=Invalid credentials 8 | welcome.msg=Welcome 9 | logout.text=Logout 10 | logout.msg.success=You have been successfully logged out. 11 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | 8 | <fmt:bundle basename="messages"> 9 | <fmt:message key="page.title" /> 10 | </fmt:bundle> 11 | 12 | 13 |
14 |

${title}

15 |

${message}

16 | 17 |

18 | 19 | 20 | 21 | : ${pageContext.request.userPrincipal.name} | 23 | 24 | 25 |

26 |
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 | 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | 11 | 12 |
13 |

14 | 15 | 16 | 17 |

18 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication-userdetailsservice/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | " /> 12 | 13 | 14 |
15 |

16 | 17 | 18 | 19 |

20 | 21 |
${error}
22 |
23 | 24 |
${msg}
25 |
26 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 41 |
User:
Password:
42 | 44 |
45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-jdbc-authentication/ 2 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/java/com/roytuts/spring/security/jdbc/authentication/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebSecurityConfig.class, WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/java/com/roytuts/spring/security/jdbc/authentication/config/SecurityWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.jdbc.authentication.config; 2 | 3 | import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 4 | 5 | public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | page.title=Spring Security JDBC Authentication 2 | page.home.heading=Home Page 3 | page.login.heading=Login Here 4 | page.admin.heading=Administrator Control Panel 5 | page.admin.message=This page demonstrates how to use Spring security. 6 | page.goto.admin=Go to Administrator page 7 | login.failure.reason=Invalid credentials 8 | welcome.msg=Welcome 9 | logout.text=Logout 10 | logout.msg.success=You have been successfully logged out. 11 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/webapp/static/css/style.css: -------------------------------------------------------------------------------- 1 | .error { 2 | padding: 15px; 3 | margin-bottom: 20px; 4 | border: 1px solid transparent; 5 | border-radius: 4px; 6 | color: #a94442; 7 | background-color: #f2dede; 8 | border-color: #ebccd1; 9 | } 10 | 11 | .msg { 12 | padding: 15px; 13 | margin-bottom: 20px; 14 | border: 1px solid transparent; 15 | border-radius: 4px; 16 | color: #31708f; 17 | background-color: #d9edf7; 18 | border-color: #bce8f1; 19 | } 20 | 21 | #login-box { 22 | width: 500px; 23 | padding: 20px; 24 | margin: 50px auto; 25 | background: #fff; 26 | -webkit-border-radius: 2px; 27 | -moz-border-radius: 2px; 28 | border: 1px solid #000; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/webapp/views/admin.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | 8 | <fmt:bundle basename="messages"> 9 | <fmt:message key="page.title" /> 10 | </fmt:bundle> 11 | 12 | 13 |
14 |

${title}

15 |

${message}

16 | 17 |

18 | 19 | 20 | 21 | : ${pageContext.request.userPrincipal.name} | 23 | 24 | 25 |

26 |
27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/webapp/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 | pageEncoding="ISO-8859-1"%> 3 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4 | 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | 11 | 12 |
13 |

14 | 15 | 16 | 17 |

18 | 20 | 21 | 22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/src/main/webapp/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" session="true" 2 | contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> 5 | 6 | 7 | <fmt:bundle basename="messages"> 8 | <fmt:message key="page.title" /> 9 | </fmt:bundle> 10 | " /> 12 | 13 | 14 |
15 |

16 | 17 | 18 | 19 |

20 | 21 |
${error}
22 |
23 | 24 |
${msg}
25 |
26 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 40 | 41 |
User:
Password:
42 | 44 |
45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-security-jdbc-authentication/user.sql: -------------------------------------------------------------------------------- 1 | USE `roytuts`; 2 | /*Table structure for table `user` */ 3 | DROP TABLE IF EXISTS `user`; 4 | CREATE TABLE `user` ( 5 | `user_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 6 | `user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, 7 | `enable` tinyint NOT NULL DEFAULT '1', 8 | PRIMARY KEY (`user_name`) 9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 | /*Data for the table `user` */ 11 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('roy','$2a$12$PN0MjtyNGWW.AjWdIuYKxe4.4Grjs4K7oanuAnt/WSDSvFlUc3eQi',1); 12 | 13 | /*Table structure for table `user_role` */ 14 | DROP TABLE IF EXISTS `user_role`; 15 | CREATE TABLE `user_role` ( 16 | `user_name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, 17 | `user_role` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL, 18 | KEY `fk_user` (`user_name`), 19 | CONSTRAINT `fk_user` FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 20 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 21 | /*Data for the table `user_role` */ 22 | insert into `user_role`(`user_name`,`user_role`) values ('roy','ROLE_ADMIN'); 23 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.5.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 28 | compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}") 29 | runtime("com.h2database:h2:1.4.196") 30 | } -------------------------------------------------------------------------------- /spring-security-pre-authentication/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-security-pre-authentication-example/ 2 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/SpringPreAuthentication.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringPreAuthentication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringPreAuthentication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.dao.EmptyResultDataAccessException; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.roytuts.spring.security.preauth.jdbc.row.mapper.UserRowMapper; 9 | import com.roytuts.spring.security.preauth.model.User; 10 | 11 | @Repository 12 | public class UserDao { 13 | 14 | @Autowired 15 | private JdbcTemplate jdbcTemplate; 16 | 17 | public User getUser(String username) { 18 | try { 19 | final String sql = "select u.user_name user_name, u.user_pass user_pass, ur.user_role user_role from user u, user_role ur where u.user_name = ? and u.user_name = ur.user_name"; 20 | User userDetails = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username); 21 | return userDetails; 22 | } catch (EmptyResultDataAccessException ex) { 23 | return null;// should have proper handling of Exception 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/jdbc/row/mapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.jdbc.row.mapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.security.preauth.model.User; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public User mapRow(ResultSet rs, int row) throws SQLException { 14 | User userDetails = new User(); 15 | userDetails.setUsername(rs.getString("user_name")); 16 | userDetails.setPassword(rs.getString("user_pass")); 17 | userDetails.setRole(rs.getString("user_role")); 18 | return userDetails; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/model/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.model; 2 | 3 | public class User { 4 | 5 | private String username; 6 | private String password; 7 | private String role; 8 | 9 | public String getUsername() { 10 | return username; 11 | } 12 | 13 | public void setUsername(String username) { 14 | this.username = username; 15 | } 16 | 17 | public String getPassword() { 18 | return password; 19 | } 20 | 21 | public void setPassword(String password) { 22 | this.password = password; 23 | } 24 | 25 | public String getRole() { 26 | return role; 27 | } 28 | 29 | public void setRole(String role) { 30 | this.role = role; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/service/CustomUserDetailsService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.service; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.security.core.GrantedAuthority; 7 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 8 | import org.springframework.security.core.userdetails.UserDetails; 9 | import org.springframework.security.core.userdetails.UserDetailsService; 10 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 11 | import org.springframework.stereotype.Service; 12 | 13 | import com.roytuts.spring.security.preauth.dao.UserDao; 14 | import com.roytuts.spring.security.preauth.model.User; 15 | 16 | @Service 17 | public class CustomUserDetailsService implements UserDetailsService { 18 | 19 | @Autowired 20 | private UserDao userDao; 21 | 22 | @Override 23 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 24 | User user = userDao.getUser(username); 25 | if (user == null) {// should have proper handling of Exception 26 | throw new UsernameNotFoundException("User '" + username + "' not found."); 27 | } 28 | GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(user.getRole()); 29 | UserDetails details = new org.springframework.security.core.userdetails.User(user.getUsername(), 30 | user.getPassword(), Arrays.asList(grantedAuthority)); 31 | return details; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/java/com/roytuts/spring/security/preauth/vo/BlogVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.vo; 2 | 3 | import java.util.Date; 4 | 5 | import com.fasterxml.jackson.annotation.JsonFormat; 6 | 7 | public class BlogVo { 8 | 9 | private String title; 10 | private String author; 11 | @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z") 12 | private Date date; 13 | private String text; 14 | 15 | public String getTitle() { 16 | return title; 17 | } 18 | 19 | public void setTitle(String title) { 20 | this.title = title; 21 | } 22 | 23 | public String getAuthor() { 24 | return author; 25 | } 26 | 27 | public void setAuthor(String author) { 28 | this.author = author; 29 | } 30 | 31 | public Date getDate() { 32 | return date; 33 | } 34 | 35 | public void setDate(Date date) { 36 | this.date = date; 37 | } 38 | 39 | public String getText() { 40 | return text; 41 | } 42 | 43 | public void setText(String text) { 44 | this.text = text; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/resources/create-table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `user` ( 2 | `user_name` varchar(30) NOT NULL, 3 | `user_pass` varchar(255) NOT NULL, 4 | `enable` tinyint(1) NOT NULL DEFAULT '1', 5 | PRIMARY KEY (`user_name`) 6 | ); 7 | 8 | DROP TABLE IF EXISTS `user_role`; 9 | 10 | CREATE TABLE `user_role` ( 11 | `user_name` varchar(30) NOT NULL, 12 | `user_role` varchar(15) NOT NULL, 13 | FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 14 | ); -------------------------------------------------------------------------------- /spring-security-pre-authentication/src/main/resources/insert-data.sql: -------------------------------------------------------------------------------- 1 | insert into `user`(`user_name`,`user_pass`,`enable`) values ('roy','$2a$12$PN0MjtyNGWW.AjWdIuYKxe4.4Grjs4K7oanuAnt/WSDSvFlUc3eQi',1); 2 | insert into `user_role`(`user_name`,`user_role`) values ('roy','ROLE_ADMIN'); 3 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/preauthorize-annotation-haspermission-example-in-spring-security/ 2 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/SpringSecurityPreauthHasPermissionApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityPreauthHasPermissionApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityPreauthHasPermissionApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/config/PasswordConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class PasswordConfig { 10 | 11 | @Bean 12 | public PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.dao; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.stream.Collectors; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.jdbc.core.JdbcTemplate; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import com.roytuts.spring.security.preauth.model.Role; 12 | import com.roytuts.spring.security.preauth.model.User; 13 | import com.roytuts.spring.security.preauthy.row.mapper.UserRowMapper; 14 | 15 | @Repository 16 | public class UserDao { 17 | 18 | @Autowired 19 | private JdbcTemplate jdbcTemplate; 20 | 21 | public User getUser(String username) { 22 | return jdbcTemplate.queryForObject("select user_name, user_pass from user where user_name = ?", 23 | new UserRowMapper(), username); 24 | } 25 | 26 | public List getRoles(String username) { 27 | List> results = jdbcTemplate 28 | .queryForList("select user_role from user_role where user_name = ?", new Object[] { username }); 29 | 30 | List roles = results.stream().map(m -> { 31 | Role role = new Role(); 32 | role.setRole(String.valueOf(m.get("user_role"))); 33 | return role; 34 | }).collect(Collectors.toList()); 35 | 36 | return roles; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.model; 2 | 3 | public class Role { 4 | 5 | private String role; 6 | 7 | public String getRole() { 8 | return role; 9 | } 10 | 11 | public void setRole(String role) { 12 | this.role = role; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/model/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.model; 2 | 3 | public class User { 4 | 5 | private String username; 6 | private String userpwd; 7 | 8 | public String getUsername() { 9 | return username; 10 | } 11 | 12 | public void setUsername(String username) { 13 | this.username = username; 14 | } 15 | 16 | public String getUserpwd() { 17 | return userpwd; 18 | } 19 | 20 | public void setUserpwd(String userpwd) { 21 | this.userpwd = userpwd; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/permission/CustomPermissionEvaluator.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.permission; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.springframework.security.access.PermissionEvaluator; 6 | import org.springframework.security.core.Authentication; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Component 10 | public class CustomPermissionEvaluator implements PermissionEvaluator { 11 | 12 | @Override 13 | public boolean hasPermission(Authentication authentication, Object accessType, Object permission) { 14 | if (authentication != null && accessType instanceof String) { 15 | if ("hasAccess".equalsIgnoreCase(String.valueOf(accessType))) { 16 | boolean hasAccess = validateAccess(String.valueOf(permission)); 17 | return hasAccess; 18 | } 19 | return false; 20 | } 21 | return false; 22 | } 23 | 24 | private boolean validateAccess(String permission) { 25 | // ideally should be checked with user role, permission in database 26 | if ("READ".equalsIgnoreCase(permission)) { 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | @Override 33 | public boolean hasPermission(Authentication authentication, Serializable serializable, String targetType, 34 | Object permission) { 35 | return false; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/rest/controller/PreAuthorizeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.security.access.prepost.PreAuthorize; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class PreAuthorizeRestController { 11 | 12 | @GetMapping("/user") 13 | @PreAuthorize("hasRole('USER')") 14 | public ResponseEntity userRole() { 15 | return new ResponseEntity("You have USER role", HttpStatus.OK); 16 | } 17 | 18 | @GetMapping("/admin") 19 | @PreAuthorize("hasRole('ADMIN')") 20 | public ResponseEntity adminRole() { 21 | return new ResponseEntity("You have ADMIN role", HttpStatus.OK); 22 | } 23 | 24 | @GetMapping("/admin/access") 25 | @PreAuthorize("hasRole('ADMIN') and hasPermission('hasAccess','READ')") 26 | public ResponseEntity adminAccess() { 27 | return new ResponseEntity("You have ADMIN role and READ access", HttpStatus.OK); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/java/com/roytuts/spring/security/preauth/row/mapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauthy.row.mapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.security.preauth.model.User; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public User mapRow(ResultSet rs, int rowNum) throws SQLException { 14 | User user = new User(); 15 | user.setUsername(rs.getString("user_name")); 16 | user.setUserpwd(rs.getString("user_pass")); 17 | return user; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-permission/user_role.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `user` ( 2 | `user_name` varchar(30) NOT NULL, 3 | `user_pass` varchar(255) NOT NULL, 4 | `enable` tinyint NOT NULL DEFAULT '1', 5 | PRIMARY KEY (`user_name`) 6 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 7 | 8 | /*!40000 ALTER TABLE `user` DISABLE KEYS */; 9 | INSERT INTO `user` (`user_name`, `user_pass`, `enable`) VALUES 10 | ('admin', '$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK', 1), 11 | ('user', '$2a$10$9Xn39aPf4LhDpRGNWvDFqu.T5ZPHbyh8iNQDSb4aNSnLqE2u2efIu', 1); 12 | 13 | CREATE TABLE IF NOT EXISTS `user_role` ( 14 | `user_name` varchar(30) NOT NULL, 15 | `user_role` varchar(15) NOT NULL, 16 | KEY `user_name` (`user_name`), 17 | CONSTRAINT `user_role_ibfk_1` FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 18 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 19 | 20 | INSERT INTO `user_role` (`user_name`, `user_role`) VALUES 21 | ('user', 'ROLE_USER'), 22 | ('admin', 'ROLE_USER'), 23 | ('admin', 'ROLE_ADMIN'); 24 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.4.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version '2.2.4.RELEASE' 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 29 | implementation("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 30 | implementation("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}") 31 | implementation('mysql:mysql-connector-java:8.0.17') 32 | //required only if jdk 9 or higher version is used 33 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 34 | } 35 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/preauthorize-annotation-hasrole-example-in-spring-security/ 2 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/SpringSecurityPreauthHasRoleApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSecurityPreauthHasRoleApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSecurityPreauthHasRoleApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/config/DatabaseConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.config; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.core.env.Environment; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 11 | 12 | @Configuration 13 | public class DatabaseConfig { 14 | 15 | @Autowired 16 | private Environment environment; 17 | 18 | @Bean 19 | public DataSource dataSource() { 20 | DriverManagerDataSource dataSource = new DriverManagerDataSource(); 21 | 22 | dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName")); 23 | dataSource.setUrl(environment.getProperty("jdbc.url")); 24 | dataSource.setUsername(environment.getProperty("jdbc.username")); 25 | dataSource.setPassword(environment.getProperty("jdbc.password")); 26 | 27 | return dataSource; 28 | } 29 | 30 | @Bean 31 | public JdbcTemplate jdbcTemplate() { 32 | return new JdbcTemplate(dataSource()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/config/EncoderConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class EncoderConfig { 10 | 11 | @Bean 12 | PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/config/SpringPreAuthorizeSecurityConfig_2.7.5.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.crypto.password.PasswordEncoder; 9 | 10 | import com.roytuts.spring.security.preauth.service.UserAuthService; 11 | 12 | @Configuration 13 | @EnableWebSecurity 14 | @EnableGlobalMethodSecurity(prePostEnabled = true) 15 | public class SpringPreAuthorizeSecurityConfig { 16 | 17 | @Autowired 18 | private UserAuthService userAuthService; 19 | 20 | @Autowired 21 | private PasswordEncoder passwordEncoder; 22 | 23 | @Autowired 24 | public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception { 25 | auth.userDetailsService(userAuthService).passwordEncoder(passwordEncoder); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.dao; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.stream.Collectors; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.jdbc.core.JdbcTemplate; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import com.roytuts.spring.security.preauth.model.Role; 12 | import com.roytuts.spring.security.preauth.model.User; 13 | import com.roytuts.spring.security.preauth.row.mapper.UserRowMapper; 14 | 15 | @Repository 16 | public class UserDao { 17 | 18 | @Autowired 19 | private JdbcTemplate jdbcTemplate; 20 | 21 | public User getUser(String username) { 22 | return jdbcTemplate.queryForObject("select user_name, user_pass from user where user_name = ?", 23 | new Object[] { username }, new UserRowMapper()); 24 | } 25 | 26 | public List getRoles(String username) { 27 | List> results = jdbcTemplate 28 | .queryForList("select user_role from user_role where user_name = ?", new Object[] { username }); 29 | List roles = results.stream().map(m -> { 30 | Role role = new Role(); 31 | role.setRole(String.valueOf(m.get("user_role"))); 32 | return role; 33 | }).collect(Collectors.toList()); 34 | return roles; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/dao/UserDao_2.7.5.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.dao; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.stream.Collectors; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.jdbc.core.JdbcTemplate; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import com.roytuts.spring.security.preauth.model.Role; 12 | import com.roytuts.spring.security.preauth.model.User; 13 | import com.roytuts.spring.security.preauth.row.mapper.UserRowMapper; 14 | 15 | @Repository 16 | public class UserDao { 17 | 18 | @Autowired 19 | private JdbcTemplate jdbcTemplate; 20 | 21 | public User getUser(String username) { 22 | return jdbcTemplate.queryForObject("select user_name, user_pass from user where user_name = ?", 23 | new UserRowMapper(), username); 24 | } 25 | 26 | public List getRoles(String username) { 27 | List> results = jdbcTemplate 28 | .queryForList("select user_role from user_role where user_name = ?", new Object[] { username }); 29 | List roles = results.stream().map(m -> { 30 | Role role = new Role(); 31 | role.setRole(String.valueOf(m.get("user_role"))); 32 | return role; 33 | }).collect(Collectors.toList()); 34 | return roles; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.model; 2 | 3 | public class Role { 4 | 5 | private String role; 6 | 7 | public String getRole() { 8 | return role; 9 | } 10 | 11 | public void setRole(String role) { 12 | this.role = role; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/model/User.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.model; 2 | 3 | public class User { 4 | 5 | private String username; 6 | private String userpwd; 7 | 8 | public String getUsername() { 9 | return username; 10 | } 11 | 12 | public void setUsername(String username) { 13 | this.username = username; 14 | } 15 | 16 | public String getUserpwd() { 17 | return userpwd; 18 | } 19 | 20 | public void setUserpwd(String userpwd) { 21 | this.userpwd = userpwd; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/rest/controller/PreAuthorizeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.security.access.prepost.PreAuthorize; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class PreAuthorizeRestController { 12 | 13 | @GetMapping("/user") 14 | @PreAuthorize("hasRole('USER')") 15 | public ResponseEntity defaultPage(Model model) { 16 | return new ResponseEntity("You have USER role.", HttpStatus.OK); 17 | } 18 | 19 | @GetMapping("/admin") 20 | @PreAuthorize("hasRole('ADMIN')") 21 | public ResponseEntity getAllBlogs(Model model) { 22 | return new ResponseEntity("You have ADMIN role.", HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/java/com/roytuts/spring/security/preauth/row/mapper/UserRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.security.preauth.row.mapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import org.springframework.jdbc.core.RowMapper; 7 | 8 | import com.roytuts.spring.security.preauth.model.User; 9 | 10 | public class UserRowMapper implements RowMapper { 11 | 12 | @Override 13 | public User mapRow(ResultSet rs, int rowNum) throws SQLException { 14 | User user = new User(); 15 | user.setUsername(rs.getString("user_name")); 16 | user.setUserpwd(rs.getString("user_pass")); 17 | return user; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost/roytuts 3 | jdbc.username=root 4 | jdbc.password=root 5 | -------------------------------------------------------------------------------- /spring-security-preauthorize-has-role/user_role.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `user` ( 2 | `user_name` varchar(30) NOT NULL, 3 | `user_pass` varchar(255) NOT NULL, 4 | `enable` tinyint NOT NULL DEFAULT '1', 5 | PRIMARY KEY (`user_name`) 6 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 7 | 8 | /*!40000 ALTER TABLE `user` DISABLE KEYS */; 9 | INSERT INTO `user` (`user_name`, `user_pass`, `enable`) VALUES 10 | ('admin', '$2a$10$dl8TemMlPH7Z/mpBurCX8O4lu0FoWbXnhsHTYXVsmgXyzagn..8rK', 1), 11 | ('user', '$2a$10$9Xn39aPf4LhDpRGNWvDFqu.T5ZPHbyh8iNQDSb4aNSnLqE2u2efIu', 1); 12 | 13 | CREATE TABLE IF NOT EXISTS `user_role` ( 14 | `user_name` varchar(30) NOT NULL, 15 | `user_role` varchar(15) NOT NULL, 16 | KEY `user_name` (`user_name`), 17 | CONSTRAINT `user_role_ibfk_1` FOREIGN KEY (`user_name`) REFERENCES `user` (`user_name`) 18 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 19 | 20 | INSERT INTO `user_role` (`user_name`, `user_role`) VALUES 21 | ('user', 'ROLE_USER'), 22 | ('admin', 'ROLE_USER'), 23 | ('admin', 'ROLE_ADMIN'); 24 | -------------------------------------------------------------------------------- /spring-security-simple-auth/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | spring-security-simple-auth 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 11 13 | 11 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.6.7 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-security 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-maven-plugin 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /spring-security-simple-auth/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/error-bean-securityconfig-unresolvable-circular-reference/ 2 | -------------------------------------------------------------------------------- /spring-security-simple-auth/src/main/java/com/roytuts/springsecurity/simpleauth/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springsecurity.simpleauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-security-simple-auth/src/main/java/com/roytuts/springsecurity/simpleauth/config/EncoderConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springsecurity.simpleauth.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | 8 | @Configuration 9 | public class EncoderConfig { 10 | 11 | @Bean 12 | PasswordEncoder passwordEncoder() { 13 | return new BCryptPasswordEncoder(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-security-simple-auth/src/main/java/com/roytuts/springsecurity/simpleauth/restcontroller/AppRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springsecurity.simpleauth.restcontroller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.security.access.prepost.PreAuthorize; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class AppRestController { 12 | 13 | @GetMapping("/user") 14 | @PreAuthorize("hasRole('USER')") 15 | public ResponseEntity defaultPage(Model model) { 16 | return new ResponseEntity("You have USER role.", HttpStatus.OK); 17 | } 18 | 19 | @GetMapping("/admin") 20 | @PreAuthorize("hasRole('ADMIN')") 21 | public ResponseEntity getAllBlogs(Model model) { 22 | return new ResponseEntity("You have ADMIN role.", HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /springboot-angular-security/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.2.RELEASE' 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version '2.2.2.RELEASE' 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 29 | implementation("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 30 | } 31 | -------------------------------------------------------------------------------- /springboot-angular-security/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | springboot-angular-security 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.2.2.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-security 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.apache.maven.plugins 36 | maven-compiler-plugin 37 | 3.8.1 38 | 39 | at least 8 40 | at least 8 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /springboot-angular-security/pom.xml_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | springboot-angular-security 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-security 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-angular-security/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-security-angular-login-logout/ 2 | 3 | Spring boot version 3 realted changes are kept into files appended with _3. 4 | -------------------------------------------------------------------------------- /springboot-angular-security/src/main/java/com/roytuts/springboot/angularsecurity/SpringBootAngularSecurityApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.angularsecurity; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootAngularSecurityApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootAngularSecurityApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-angular-security/src/main/java/com/roytuts/springboot/angularsecurity/config/SpringSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.angularsecurity.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.http.HttpMethod; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 | 9 | @Configuration 10 | @EnableWebSecurity 11 | public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 12 | 13 | @Override 14 | protected void configure(HttpSecurity http) throws Exception { 15 | http.csrf().disable().authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll().anyRequest() 16 | .authenticated().and().httpBasic(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /springboot-angular-security/src/main/java/com/roytuts/springboot/angularsecurity/config/SpringSecurityConfig.java_3: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.angularsecurity.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.http.HttpMethod; 6 | import org.springframework.security.config.Customizer; 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 | import org.springframework.security.web.SecurityFilterChain; 9 | 10 | @Configuration 11 | public class SpringSecurityConfig { 12 | 13 | @Bean 14 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 15 | http.csrf(csrf -> csrf.disable()); 16 | 17 | http.authorizeHttpRequests( 18 | auth -> auth.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll().anyRequest().authenticated()); 19 | 20 | http.httpBasic(Customizer.withDefaults()); 21 | 22 | return http.build(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /springboot-angular-security/src/main/java/com/roytuts/springboot/angularsecurity/rest/controller/UiRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.angularsecurity.rest.controller; 2 | 3 | import java.security.Principal; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.UUID; 7 | 8 | import org.springframework.web.bind.annotation.CrossOrigin; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | @CrossOrigin(origins = "http://localhost:4200") 14 | public class UiRestController { 15 | 16 | @GetMapping("/auth") 17 | public Principal user(Principal user) { 18 | return user; 19 | } 20 | 21 | @GetMapping("/resource") 22 | public Map home() { 23 | Map model = new HashMap(); 24 | 25 | model.put("id", UUID.randomUUID().toString()); 26 | model.put("content", "Hello World"); 27 | 28 | return model; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-angular-security/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.security.user.name=user 2 | spring.security.user.password=user 3 | -------------------------------------------------------------------------------- /springboot-react-security/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | springboot-react-security 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-security 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-react-security/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/react-login-logout-with-spring-boot-security/ 2 | -------------------------------------------------------------------------------- /springboot-react-security/src/main/java/com/roytuts/springboot/reactsecurity/SpringBootReactSecurityApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.reactsecurity; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootReactSecurityApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootReactSecurityApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-react-security/src/main/java/com/roytuts/springboot/reactsecurity/config/SpringSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.reactsecurity.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.http.HttpMethod; 6 | import org.springframework.security.config.Customizer; 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 | import org.springframework.security.web.SecurityFilterChain; 9 | 10 | @Configuration 11 | public class SpringSecurityConfig { 12 | 13 | @Bean 14 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 15 | http.csrf(csrf -> csrf.disable()); 16 | 17 | http.authorizeHttpRequests( 18 | auth -> auth.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll().anyRequest().authenticated()); 19 | 20 | http.httpBasic(Customizer.withDefaults()); 21 | 22 | return http.build(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /springboot-react-security/src/main/java/com/roytuts/springboot/reactsecurity/rest/controller/UiRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.reactsecurity.rest.controller; 2 | 3 | import java.security.Principal; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.UUID; 7 | 8 | import org.springframework.web.bind.annotation.CrossOrigin; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | @CrossOrigin(origins = "http://localhost:3000") 14 | public class UiRestController { 15 | 16 | @GetMapping("/auth") 17 | public Principal user(Principal user) { 18 | return user; 19 | } 20 | 21 | @GetMapping("/resource") 22 | public Map home() { 23 | Map model = new HashMap(); 24 | 25 | model.put("id", UUID.randomUUID().toString()); 26 | model.put("content", "Hello World"); 27 | 28 | return model; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-react-security/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.security.user.name=user 2 | spring.security.user.password=user 3 | --------------------------------------------------------------------------------