11package com .razdeep .konsignapi .controller ;
22
33import com .razdeep .konsignapi .config .KonsignConfig ;
4+ import com .razdeep .konsignapi .constant .KonsignConstant ;
45import com .razdeep .konsignapi .exception .UsernameAlreadyExists ;
56import com .razdeep .konsignapi .model .AuthenticationRequest ;
67import com .razdeep .konsignapi .model .AuthenticationResponse ;
1011import com .razdeep .konsignapi .service .JwtUtilService ;
1112import com .razdeep .konsignapi .service .KonsignUserDetailsService ;
1213import io .jsonwebtoken .impl .DefaultClaims ;
14+ import java .util .Arrays ;
15+ import java .util .HashMap ;
16+ import java .util .Optional ;
17+ import javax .servlet .http .Cookie ;
18+ import javax .servlet .http .HttpServletRequest ;
19+ import javax .servlet .http .HttpServletResponse ;
1320import lombok .val ;
1421import org .slf4j .Logger ;
1522import org .slf4j .LoggerFactory ;
2229import org .springframework .security .core .userdetails .UserDetails ;
2330import org .springframework .web .bind .annotation .*;
2431
25- import javax .servlet .http .Cookie ;
26- import javax .servlet .http .HttpServletRequest ;
27- import javax .servlet .http .HttpServletResponse ;
28- import java .util .Arrays ;
29- import java .util .HashMap ;
30- import java .util .Optional ;
31-
32- import com .razdeep .konsignapi .constant .KonsignConstant ;
33-
3432@ CrossOrigin
35- @ RestController
33+ @ RestController ( KonsignConstant . CONTROLLER_API_PREFIX )
3634public class AuthenticationController {
3735
3836 private static final Logger LOG = LoggerFactory .getLogger (AuthenticationController .class );
@@ -43,34 +41,42 @@ public class AuthenticationController {
4341 private final AuthenticationService authenticationService ;
4442
4543 @ Autowired
46- public AuthenticationController (AuthenticationManager authenticationManager , KonsignUserDetailsService konsignUserDetailsService , JwtUtilService jwtUtilService , AuthenticationService authenticationService ) {
44+ public AuthenticationController (
45+ AuthenticationManager authenticationManager ,
46+ KonsignUserDetailsService konsignUserDetailsService ,
47+ JwtUtilService jwtUtilService ,
48+ AuthenticationService authenticationService ) {
4749 this .authenticationManager = authenticationManager ;
4850 this .konsignUserDetailsService = konsignUserDetailsService ;
4951 this .jwtUtilService = jwtUtilService ;
5052 this .authenticationService = authenticationService ;
5153 }
5254
5355 @ PostMapping (value = "/authenticate" )
54- public ResponseEntity <?> authenticate (@ RequestBody AuthenticationRequest authenticationRequest , HttpServletResponse response ) {
56+ public ResponseEntity <?> authenticate (
57+ @ RequestBody AuthenticationRequest authenticationRequest , HttpServletResponse response ) {
5558 try {
56- authenticationManager .authenticate (new UsernamePasswordAuthenticationToken (authenticationRequest . getUsername (),
57- authenticationRequest .getPassword ()));
59+ authenticationManager .authenticate (new UsernamePasswordAuthenticationToken (
60+ authenticationRequest .getUsername (), authenticationRequest . getPassword ()));
5861 } catch (BadCredentialsException e ) {
5962 return ResponseEntity .badRequest ().body ("Username or password mismatch" );
6063 } catch (Exception e ) {
6164 e .printStackTrace ();
6265 }
6366
64- final UserDetails konsignUserDetails = konsignUserDetailsService .loadUserByUsername (authenticationRequest .getUsername ());
67+ final UserDetails konsignUserDetails =
68+ konsignUserDetailsService .loadUserByUsername (authenticationRequest .getUsername ());
6569 final String accessToken = jwtUtilService .generateToken (konsignUserDetails );
6670 final AuthenticationResponse authenticationResponse = new AuthenticationResponse ();
6771 authenticationResponse .setAccessToken (accessToken );
6872
69- Cookie cookie = new Cookie (KonsignConstant .HEADER_REFRESH_TOKEN , jwtUtilService .doGenerateRefreshToken (new HashMap <>(), authenticationRequest .getUsername ()));
73+ Cookie cookie = new Cookie (
74+ KonsignConstant .HEADER_REFRESH_TOKEN ,
75+ jwtUtilService .doGenerateRefreshToken (new HashMap <>(), authenticationRequest .getUsername ()));
7076
7177 cookie .setMaxAge (KonsignConfig .cookieMaxAge );
7278
73- // cookie.setSecure(true);
79+ // cookie.setSecure(true);
7480 cookie .setHttpOnly (KonsignConfig .cookieHttpOnly );
7581 cookie .setPath (KonsignConfig .cookiePath );
7682
@@ -99,7 +105,9 @@ public ResponseEntity<?> refreshToken(HttpServletRequest request, HttpServletRes
99105
100106 try {
101107 if (jwtUtilService .validateToken (refreshToken , null )) {
102- // jwtUtilService.validateToken(jwtUtilService.extractAccessTokenFromRequest(request), null);
108+ //
109+ // jwtUtilService.validateToken(jwtUtilService.extractAccessTokenFromRequest(request),
110+ // null);
103111 DefaultClaims claims = (DefaultClaims ) request .getAttribute (KonsignConstant .HEADER_CLAIMS );
104112 val claimsMap = jwtUtilService .getMapFromIoJsonWebTokenClaims (claims );
105113 String jwtToken = jwtUtilService .doGenerateRefreshToken (claimsMap , (String ) claimsMap .get ("sub" ));
@@ -126,4 +134,9 @@ public ResponseEntity<ResponseVerdict> register(@RequestBody UserRegistration us
126134 responseVerdict .setMessage ("Successfully registered" );
127135 return new ResponseEntity <>(responseVerdict , HttpStatus .OK );
128136 }
137+
138+ @ GetMapping (value = "/" )
139+ public ResponseEntity <String > welcome () {
140+ return new ResponseEntity <>("Welcome to konsign-api" , HttpStatus .OK );
141+ }
129142}
0 commit comments