diff --git a/.idea/libraries/Maven__org_attoparser_attoparser_2_0_5_RELEASE.xml b/.idea/libraries/Maven__org_attoparser_attoparser_2_0_5_RELEASE.xml new file mode 100644 index 0000000..5bad7db --- /dev/null +++ b/.idea/libraries/Maven__org_attoparser_attoparser_2_0_5_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_thymeleaf_2_3_0_RELEASE.xml b/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_thymeleaf_2_3_0_RELEASE.xml new file mode 100644 index 0000000..268662c --- /dev/null +++ b/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_thymeleaf_2_3_0_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_thymeleaf_extras_thymeleaf_extras_java8time_3_0_4_RELEASE.xml b/.idea/libraries/Maven__org_thymeleaf_extras_thymeleaf_extras_java8time_3_0_4_RELEASE.xml new file mode 100644 index 0000000..7b246ae --- /dev/null +++ b/.idea/libraries/Maven__org_thymeleaf_extras_thymeleaf_extras_java8time_3_0_4_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_thymeleaf_thymeleaf_3_0_11_RELEASE.xml b/.idea/libraries/Maven__org_thymeleaf_thymeleaf_3_0_11_RELEASE.xml new file mode 100644 index 0000000..7b72bab --- /dev/null +++ b/.idea/libraries/Maven__org_thymeleaf_thymeleaf_3_0_11_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_thymeleaf_thymeleaf_spring5_3_0_11_RELEASE.xml b/.idea/libraries/Maven__org_thymeleaf_thymeleaf_spring5_3_0_11_RELEASE.xml new file mode 100644 index 0000000..3370587 --- /dev/null +++ b/.idea/libraries/Maven__org_thymeleaf_thymeleaf_spring5_3_0_11_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_unbescape_unbescape_1_1_6_RELEASE.xml b/.idea/libraries/Maven__org_unbescape_unbescape_1_1_6_RELEASE.xml new file mode 100644 index 0000000..2334aa0 --- /dev/null +++ b/.idea/libraries/Maven__org_unbescape_unbescape_1_1_6_RELEASE.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d14b2aa..aaad21d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,13 +2,27 @@ - - - - + + + + + + + + + + + + + + + + + + @@ -47,6 +62,7 @@ + @@ -96,7 +112,7 @@ - + 1589796661163 @@ -144,42 +160,42 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + @@ -200,13 +216,17 @@ + + + + - + - + \ No newline at end of file diff --git a/jdbc.iml b/jdbc.iml index 7999556..d39a43e 100644 --- a/jdbc.iml +++ b/jdbc.iml @@ -129,6 +129,12 @@ + + + + + + diff --git a/pom.xml b/pom.xml index b9b4b07..b8fbc16 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,13 @@ postgresql 42.2.12 + + + org.springframework.boot + spring-boot-starter-thymeleaf + 2.3.0.RELEASE + + @@ -45,7 +52,6 @@ spring-boot-starter-security 2.3.0.RELEASE - org.jetbrains.kotlin kotlin-reflect diff --git a/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java b/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java index f3620df..e637a2f 100644 --- a/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java +++ b/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java @@ -1,6 +1,7 @@ package com.chantha.jdbc.config; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -10,6 +11,8 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @@ -18,6 +21,8 @@ public class WebConfig extends WebSecurityConfigurerAdapter { private final UserDetailsService userDetailsService; + + @Autowired public WebConfig(UserDetailsService userDetailsService){ this.userDetailsService=userDetailsService; @@ -25,7 +30,7 @@ public class WebConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.userDetailsService(userDetailsService); + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override @@ -33,10 +38,16 @@ public class WebConfig extends WebSecurityConfigurerAdapter { http.formLogin(); http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")); http.authorizeRequests() + .antMatchers("/register").permitAll() .antMatchers("/**").hasAnyRole("ADMIN"); http.csrf().disable(); } + @Bean + public PasswordEncoder passwordEncoder(){ + return new BCryptPasswordEncoder(); + } + diff --git a/src/main/kotlin/com/chantha/jdbc/controller/UserController.kt b/src/main/kotlin/com/chantha/jdbc/controller/UserController.kt new file mode 100644 index 0000000..bbeb326 --- /dev/null +++ b/src/main/kotlin/com/chantha/jdbc/controller/UserController.kt @@ -0,0 +1,37 @@ +package com.chantha.jdbc.controller + +import com.chantha.jdbc.security.User +import com.chantha.jdbc.security.UserRepo +import com.sun.istack.NotNull +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.ModelAttribute +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestParam + + +@Controller +class UserController @Autowired constructor(private val userRepo: UserRepo){ + + fun encoder():BCryptPasswordEncoder{ + return BCryptPasswordEncoder() + } + + + + + @GetMapping("/register") + fun register():String{ + return "register" + } + + @PostMapping("/register") + fun saveRegister(@NotNull @ModelAttribute("user") user:User?):String{ + user!!.password=encoder().encode(user.password) + user.roles="ROLES_${user.roles}" + userRepo.save(user) + return "register" + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/chantha/jdbc/security/User.kt b/src/main/kotlin/com/chantha/jdbc/security/User.kt index 5a1c2f7..af9b57d 100644 --- a/src/main/kotlin/com/chantha/jdbc/security/User.kt +++ b/src/main/kotlin/com/chantha/jdbc/security/User.kt @@ -8,14 +8,14 @@ import javax.persistence.* @Table(name = "tbUser") data class User( @Id - @GeneratedValue - var userId:Long , + @GeneratedValue(strategy = GenerationType.IDENTITY) + var userId:Long ? = 0 , @Column(nullable = false,unique = true) var userName:String, @Column(nullable = false,unique = true) var password:String, var roles:String, - var status:Int + var status:Int ?= 1 ) { diff --git a/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt b/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt index f466183..661b853 100644 --- a/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt +++ b/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt @@ -7,8 +7,10 @@ import org.springframework.stereotype.Service @Service class UserDetailServiceImpl @Autowired constructor(private val userRepo: UserRepo):UserDetailsService { + @Throws(Exception::class) override fun loadUserByUsername(p0: String?): UserDetails { val user=userRepo.findByUsername(p0!!) return UserPrincipal(user) } + } \ No newline at end of file diff --git a/src/main/kotlin/com/chantha/jdbc/security/UserPrincipal.kt b/src/main/kotlin/com/chantha/jdbc/security/UserPrincipal.kt index c7997dd..d3f3320 100644 --- a/src/main/kotlin/com/chantha/jdbc/security/UserPrincipal.kt +++ b/src/main/kotlin/com/chantha/jdbc/security/UserPrincipal.kt @@ -33,7 +33,7 @@ class UserPrincipal constructor(private val user: User):UserDetails{ } override fun getPassword(): String { - return "{noop}${user.password}" + return user.password } override fun isAccountNonExpired(): Boolean { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ad18dc6..5a53e97 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,6 +5,7 @@ spring.jpa.hibernate.ddl-auto=update spring.jpa.open-in-view=true server.port=8081 +spring.thymeleaf.cache=true diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html new file mode 100644 index 0000000..9d25551 --- /dev/null +++ b/src/main/resources/templates/register.html @@ -0,0 +1,38 @@ + + + + + + + + Document + + + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/target/classes/application.properties b/target/classes/application.properties index ad18dc6..5a53e97 100644 --- a/target/classes/application.properties +++ b/target/classes/application.properties @@ -5,6 +5,7 @@ spring.jpa.hibernate.ddl-auto=update spring.jpa.open-in-view=true server.port=8081 +spring.thymeleaf.cache=true diff --git a/target/classes/com/chantha/jdbc/config/WebConfig.class b/target/classes/com/chantha/jdbc/config/WebConfig.class index d6c3180..aaba873 100644 Binary files a/target/classes/com/chantha/jdbc/config/WebConfig.class and b/target/classes/com/chantha/jdbc/config/WebConfig.class differ diff --git a/target/classes/com/chantha/jdbc/controller/UserController.class b/target/classes/com/chantha/jdbc/controller/UserController.class new file mode 100644 index 0000000..1371a68 Binary files /dev/null and b/target/classes/com/chantha/jdbc/controller/UserController.class differ diff --git a/target/classes/com/chantha/jdbc/security/User.class b/target/classes/com/chantha/jdbc/security/User.class index 6721d64..ac5dfed 100644 Binary files a/target/classes/com/chantha/jdbc/security/User.class and b/target/classes/com/chantha/jdbc/security/User.class differ diff --git a/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class b/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class index 6f6a9d1..f7af3af 100644 Binary files a/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class and b/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class differ diff --git a/target/classes/com/chantha/jdbc/security/UserPrincipal.class b/target/classes/com/chantha/jdbc/security/UserPrincipal.class index 6338e3b..c80f7c5 100644 Binary files a/target/classes/com/chantha/jdbc/security/UserPrincipal.class and b/target/classes/com/chantha/jdbc/security/UserPrincipal.class differ diff --git a/target/classes/templates/register.html b/target/classes/templates/register.html new file mode 100644 index 0000000..9d25551 --- /dev/null +++ b/target/classes/templates/register.html @@ -0,0 +1,38 @@ + + + + + + + + Document + + + +
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ + \ No newline at end of file