Spring Security UserDetailSerivce With ROLE

This commit is contained in:
Chantha 2020-05-19 13:15:35 +07:00
parent bf58ea2903
commit 961b318402
12 changed files with 180 additions and 50 deletions

View File

@ -2,14 +2,13 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="47c3fbf4-4238-47d3-9ffa-fcd23da4e01e" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_security_2_3_0_RELEASE.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_springframework_security_spring_security_config_5_2_4_RELEASE.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_springframework_security_spring_security_core_5_2_4_RELEASE.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/libraries/Maven__org_springframework_security_spring_security_web_5_2_4_RELEASE.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/security/User.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/security/UserPrincipal.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/security/UserRepo.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/jdbc.iml" beforeDir="false" afterPath="$PROJECT_DIR$/jdbc.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/config/WebConfig.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/application.properties" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -20,10 +19,10 @@
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Kotlin Interface" />
<option value="Kotlin Class" />
<option value="Kotlin File" />
<option value="Class" />
<option value="Kotlin Class" />
<option value="Kotlin Interface" />
<option value="Kotlin File" />
</list>
</option>
</component>
@ -56,7 +55,7 @@
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../springInterview" />
<property name="restartRequiresConfirmation" value="false" />
</component>
<component name="RecentsManager">
@ -95,13 +94,27 @@
<workItem from="1589690218526" duration="3238000" />
<workItem from="1589695280458" duration="9667000" />
<workItem from="1589774040527" duration="72000" />
<workItem from="1589788206287" duration="3926000" />
<workItem from="1589788206287" duration="4024000" />
<workItem from="1589857721932" duration="2546000" />
<workItem from="1589862025987" duration="5219000" />
</task>
<task id="LOCAL-00001" summary="&quot;Add Spring Security to project&quot;">
<created>1589796661163</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1589796661163</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="&quot;Add Spring Security to project&quot;" />
<option name="LAST_COMMIT_MESSAGE" value="&quot;Add Spring Security to project&quot;" />
</component>
<component name="WindowStateProjectService">
<state x="552" y="179" key="#Auto_Import" timestamp="1589535953579">
<screen x="0" y="0" width="1920" height="1040" />
@ -111,10 +124,10 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="690" y="276" key="#com.intellij.codeInsight.editorActions.RestoreReferencesDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1589601961718" />
<state x="765" y="197" key="#com.intellij.ide.util.MemberChooser" timestamp="1589788421060">
<state x="765" y="197" key="#com.intellij.ide.util.MemberChooser" timestamp="1589863580998">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="765" y="197" key="#com.intellij.ide.util.MemberChooser/0.0.1920.1040@0.0.1920.1040" timestamp="1589788421060" />
<state x="765" y="197" key="#com.intellij.ide.util.MemberChooser/0.0.1920.1040@0.0.1920.1040" timestamp="1589863580998" />
<state x="649" y="437" key="#com.intellij.refactoring.move.MoveHandler.SelectRefactoringDialog" timestamp="1589690277066">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -127,58 +140,62 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="739" y="31" key="#org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui.MoveKotlinTopLevelDeclarationsDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1589608464352" />
<state x="740" y="276" key="FileChooserDialogImpl" timestamp="1589774110803">
<state x="508" y="29" key="CommitChangelistDialog2" timestamp="1589796659997">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="740" y="276" key="FileChooserDialogImpl/0.0.1920.1040@0.0.1920.1040" timestamp="1589774110803" />
<state width="1877" height="420" key="GridCell.Tab.0.bottom" timestamp="1589796342290">
<state x="508" y="29" key="CommitChangelistDialog2/0.0.1920.1040@0.0.1920.1040" timestamp="1589796659997" />
<state x="740" y="276" key="FileChooserDialogImpl" timestamp="1589866688537">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.0.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.0.center" timestamp="1589796342290">
<state x="740" y="276" key="FileChooserDialogImpl/0.0.1920.1040@0.0.1920.1040" timestamp="1589866688537" />
<state width="1877" height="420" key="GridCell.Tab.0.bottom" timestamp="1589868867505">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.0.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.0.left" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.0.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867505" />
<state width="1877" height="420" key="GridCell.Tab.0.center" timestamp="1589868867505">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.0.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.0.right" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.0.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867505" />
<state width="1877" height="420" key="GridCell.Tab.0.left" timestamp="1589868867505">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.0.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.1.bottom" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.0.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867505" />
<state width="1877" height="420" key="GridCell.Tab.0.right" timestamp="1589868867505">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.1.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.1.center" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.0.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867505" />
<state width="1877" height="420" key="GridCell.Tab.1.bottom" timestamp="1589868867506">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.1.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.1.left" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.1.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867506" />
<state width="1877" height="420" key="GridCell.Tab.1.center" timestamp="1589868867506">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.1.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="420" key="GridCell.Tab.1.right" timestamp="1589796342290">
<state width="1877" height="420" key="GridCell.Tab.1.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867506" />
<state width="1877" height="420" key="GridCell.Tab.1.left" timestamp="1589868867505">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="420" key="GridCell.Tab.1.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589796342290" />
<state width="1877" height="326" key="GridCell.Tab.2.bottom" timestamp="1589788489370">
<state width="1877" height="420" key="GridCell.Tab.1.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867505" />
<state width="1877" height="420" key="GridCell.Tab.1.right" timestamp="1589868867506">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="326" key="GridCell.Tab.2.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589788489370" />
<state width="1877" height="326" key="GridCell.Tab.2.center" timestamp="1589788489370">
<state width="1877" height="420" key="GridCell.Tab.1.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589868867506" />
<state width="1877" height="326" key="GridCell.Tab.2.bottom" timestamp="1589796703748">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="326" key="GridCell.Tab.2.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589788489370" />
<state width="1877" height="326" key="GridCell.Tab.2.left" timestamp="1589788489370">
<state width="1877" height="326" key="GridCell.Tab.2.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589796703748" />
<state width="1877" height="326" key="GridCell.Tab.2.center" timestamp="1589796703748">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="326" key="GridCell.Tab.2.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589788489370" />
<state width="1877" height="326" key="GridCell.Tab.2.right" timestamp="1589788489370">
<state width="1877" height="326" key="GridCell.Tab.2.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589796703748" />
<state width="1877" height="326" key="GridCell.Tab.2.left" timestamp="1589796703748">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="326" key="GridCell.Tab.2.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589788489370" />
<state width="1877" height="326" key="GridCell.Tab.2.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589796703748" />
<state width="1877" height="326" key="GridCell.Tab.2.right" timestamp="1589796703748">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1877" height="326" key="GridCell.Tab.2.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589796703748" />
<state x="767" y="433" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1589796631131">
<screen x="0" y="0" width="1920" height="1040" />
</state>
@ -187,9 +204,9 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="616" y="240" key="run.anything.popup/0.0.1920.1040@0.0.1920.1040" timestamp="1589796621453" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1589699975397">
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1589859310268">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040@0.0.1920.1040" timestamp="1589699975397" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040@0.0.1920.1040" timestamp="1589859310268" />
</component>
</project>

View File

@ -1,27 +1,40 @@
package com.chantha.jdbc.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
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.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class WebConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
private final UserDetailsService userDetailsService;
UserDetails user= User.builder()
.username("chantha")
.password("chantha")
.roles("ADMIN")
.build();
@Autowired
public WebConfig(UserDetailsService userDetailsService){
this.userDetailsService=userDetailsService;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin();
http.cors().disable();
http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
http.authorizeRequests()
.antMatchers("/**").hasAnyRole("ADMIN");
http.csrf().disable();
}

View File

@ -0,0 +1,28 @@
package com.chantha.jdbc.security
import javax.persistence.*
@Suppress("UNCHECKED_CAST")
@Entity
@Table(name = "tbUser")
data class User(
@Id
@GeneratedValue
var userId:Long ,
@Column(nullable = false,unique = true)
var userName:String,
@Column(nullable = false,unique = true)
var password:String,
var roles:String,
var status:Int
)
{
fun getRolesList():List<String>{
if(roles.isNotEmpty()){
return roles.split("_")
}
return listOf()
}
}

View File

@ -0,0 +1,14 @@
package com.chantha.jdbc.security
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
@Service
class UserDetailServiceImpl @Autowired constructor(private val userRepo: UserRepo):UserDetailsService {
override fun loadUserByUsername(p0: String?): UserDetails {
val user=userRepo.findByUsername(p0!!)
return UserPrincipal(user)
}
}

View File

@ -0,0 +1,47 @@
package com.chantha.jdbc.security
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.stereotype.Component
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Service
class UserPrincipal constructor(private val user: User):UserDetails{
override fun getAuthorities(): List<GrantedAuthority> {
val listGrantedAuthority= mutableListOf<GrantedAuthority>()
user.getRolesList().map {
val roles=SimpleGrantedAuthority("ROLE_$it")
listGrantedAuthority.add(roles)
}
return listGrantedAuthority
}
override fun isEnabled(): Boolean {
return user.status == 1
}
override fun getUsername(): String {
return user.userName
}
override fun isCredentialsNonExpired(): Boolean {
return true
}
override fun getPassword(): String {
return "{noop}${user.password}"
}
override fun isAccountNonExpired(): Boolean {
return true
}
override fun isAccountNonLocked(): Boolean {
return true
}
}

View File

@ -0,0 +1,11 @@
package com.chantha.jdbc.security
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
@Repository
interface UserRepo : CrudRepository<User,Long>{
@Query("SELECT * FROM tb_user WHERE user_name = :user ",nativeQuery = true)
fun findByUsername(user:String):User
}

View File

@ -4,7 +4,7 @@ spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=true
server.port=8081

Binary file not shown.

Binary file not shown.