This commit is contained in:
Chantha 2020-05-19 17:05:29 +07:00
parent 4fce30fdc7
commit 0db885396d
5 changed files with 74 additions and 30 deletions

View File

@ -2,20 +2,8 @@
<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__io_jsonwebtoken_jjwt_0_9_1.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtAuthenticationEntryPoint.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtRequest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtRequestFilter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtResponse.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtTokenUtil.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/utils/jwt/JwtAuthenticationController.java" 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$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" 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/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/com/chantha/jdbc/security/UserDetailServiceImpl.kt" 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" />
<change beforePath="$PROJECT_DIR$/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/chantha/jdbc/security/UserDetailServiceImpl.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -105,7 +93,7 @@
<workItem from="1589774040527" duration="72000" />
<workItem from="1589788206287" duration="4024000" />
<workItem from="1589857721932" duration="2546000" />
<workItem from="1589862025987" duration="17207000" />
<workItem from="1589862025987" duration="17838000" />
</task>
<task id="LOCAL-00001" summary="&quot;Add Spring Security to project&quot;">
<created>1589796661163</created>
@ -161,38 +149,38 @@
<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="1589879048291" />
<state width="1877" height="420" key="GridCell.Tab.0.bottom" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.0.bottom" timestamp="1589882691520">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.0.center" timestamp="1589881169862">
<state width="1877" height="420" key="GridCell.Tab.0.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691520" />
<state width="1877" height="420" key="GridCell.Tab.0.center" timestamp="1589882691520">
<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="1589881169862" />
<state width="1877" height="420" key="GridCell.Tab.0.left" timestamp="1589881169862">
<state width="1877" height="420" key="GridCell.Tab.0.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691520" />
<state width="1877" height="420" key="GridCell.Tab.0.left" timestamp="1589882691520">
<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="1589881169862" />
<state width="1877" height="420" key="GridCell.Tab.0.right" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.0.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691520" />
<state width="1877" height="420" key="GridCell.Tab.0.right" timestamp="1589882691520">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.1.bottom" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.0.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691520" />
<state width="1877" height="420" key="GridCell.Tab.1.bottom" timestamp="1589882691521">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.1.center" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.1.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691521" />
<state width="1877" height="420" key="GridCell.Tab.1.center" timestamp="1589882691521">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.1.left" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.1.center/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691521" />
<state width="1877" height="420" key="GridCell.Tab.1.left" timestamp="1589882691521">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.1.right" timestamp="1589881169863">
<state width="1877" height="420" key="GridCell.Tab.1.left/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691521" />
<state width="1877" height="420" key="GridCell.Tab.1.right" timestamp="1589882691521">
<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="1589881169863" />
<state width="1877" height="420" key="GridCell.Tab.1.right/0.0.1920.1040@0.0.1920.1040" timestamp="1589882691521" />
<state width="1877" height="326" key="GridCell.Tab.2.bottom" timestamp="1589796703748">
<screen x="0" y="0" width="1920" height="1040" />
</state>

View File

@ -4,6 +4,7 @@ import com.chantha.jdbc.utils.jwt.JwtRequestFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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;
@ -47,6 +48,12 @@ public class WebConfig extends WebSecurityConfigurerAdapter {
http.addFilterBefore(jwtRequestFilter,UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();

View File

@ -0,0 +1,49 @@
package com.chantha.jdbc.utils.jwt;
import java.util.Objects;
import com.chantha.jdbc.security.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class JwtAuthenticationController {
private AuthenticationManager authenticationManager;
private JwtTokenUtil jwtTokenUtil;
private UserDetailServiceImpl userDetailsService;
@Autowired
public JwtAuthenticationController(AuthenticationManager authenticationManager,UserDetailServiceImpl userDetailsService,JwtTokenUtil jwtTokenUtil){
this.authenticationManager=authenticationManager;
this.jwtTokenUtil=jwtTokenUtil;
this.userDetailsService=userDetailsService;
}
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
final UserDetails userDetails = userDetailsService
.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token));
}
private void authenticate(String username, String password) throws Exception {
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (DisabledException e) {
throw new Exception("USER_DISABLED", e);
} catch (BadCredentialsException e) {
throw new Exception("INVALID_CREDENTIALS", e);
}
}
}