-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathJwtController.java
More file actions
50 lines (38 loc) · 1.88 KB
/
JwtController.java
File metadata and controls
50 lines (38 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.accenture.codingtest.springbootcodingtest.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.accenture.codingtest.springbootcodingtest.filter.JwtUtil;
import com.accenture.codingtest.springbootcodingtest.model.JwtRequestModel;
import com.accenture.codingtest.springbootcodingtest.model.JwtResponseModel;
import com.accenture.codingtest.springbootcodingtest.service.GroupUserDetailsService;
@RestController
public class JwtController {
@Autowired
private JwtUtil jwtUtil;
@Autowired
private GroupUserDetailsService groupUserDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@PostMapping(value = "/Token/GenerateToken")
public ResponseEntity<?> GenerateToken(@RequestBody JwtRequestModel jwtRequestM) throws Exception {
try {
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(jwtRequestM.getUsername(), jwtRequestM.getPassword()));
}catch(UsernameNotFoundException e)
{
e.printStackTrace();
throw new Exception("Bad Credentials");
}
//fine area
UserDetails userDetails = this.groupUserDetailsService.loadUserByUsername(jwtRequestM.getUsername());
String token = jwtUtil.generateToken(jwtRequestM.getUsername());
System.out.println(token);
return ResponseEntity.ok( new JwtResponseModel(token));
}
}