Revize ab4c8e03
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java | ||
---|---|---|
6 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
7 | 7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
8 | 8 |
import org.springframework.beans.factory.annotation.Autowired; |
9 |
import org.springframework.http.HttpHeaders; |
|
9 | 10 |
import org.springframework.http.HttpStatus; |
10 | 11 |
import org.springframework.http.ResponseEntity; |
11 | 12 |
import org.springframework.security.authentication.AuthenticationManager; |
... | ... | |
77 | 78 |
* @return message after logout |
78 | 79 |
*/ |
79 | 80 |
@PostMapping(value = "/logout") |
80 |
public ResponseEntity<String> logoutUser(@RequestBody User user) { |
|
81 |
|
|
81 |
public ResponseEntity<String> logoutUser(@RequestHeader HttpHeaders headers, @RequestBody User user) { |
|
82 |
final String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION); |
|
83 |
if(authHeader == null || !authHeader.startsWith("Bearer")){ |
|
84 |
//chyba |
|
85 |
} |
|
86 |
final String token = authHeader.substring(7); |
|
87 |
user.setToken(token); |
|
82 | 88 |
return aOuthService.logoutUser(user); |
83 | 89 |
} |
84 | 90 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java | ||
---|---|---|
1 | 1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security; |
2 | 2 |
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider; |
|
4 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService; |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl; |
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.core.annotation.Order; |
|
9 | 4 |
import org.springframework.http.*; |
10 |
import org.springframework.security.authentication.AuthenticationProvider; |
|
11 | 5 |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
12 | 6 |
import org.springframework.security.core.Authentication; |
13 | 7 |
import org.springframework.security.core.context.SecurityContextHolder; |
14 | 8 |
import org.springframework.security.core.userdetails.User; |
15 | 9 |
import org.springframework.security.core.userdetails.UserDetails; |
16 | 10 |
import org.springframework.stereotype.Component; |
17 |
import org.springframework.web.client.RestTemplate; |
|
18 | 11 |
import org.springframework.web.filter.OncePerRequestFilter; |
19 | 12 |
|
20 | 13 |
import javax.servlet.FilterChain; |
21 | 14 |
import javax.servlet.ServletException; |
22 | 15 |
import javax.servlet.http.HttpServletRequest; |
23 | 16 |
import javax.servlet.http.HttpServletResponse; |
24 |
import java.io.BufferedReader; |
|
25 |
import java.io.BufferedWriter; |
|
26 | 17 |
import java.io.IOException; |
27 |
import java.security.Security; |
|
28 | 18 |
import java.util.Collections; |
29 |
import java.util.HashMap; |
|
30 |
import java.util.Objects; |
|
31 | 19 |
|
32 | 20 |
@Component |
33 | 21 |
public class JwtAuthenticationFilter extends OncePerRequestFilter { |
34 | 22 |
|
35 |
private OAuthService oAuthService; |
|
23 |
private final OAuthService oAuthService;
|
|
36 | 24 |
|
37 | 25 |
public JwtAuthenticationFilter(OAuthService oAuthService) { |
38 | 26 |
this.oAuthService = oAuthService; |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java | ||
---|---|---|
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
public ResponseEntity<String> authenticate(String token) { |
71 |
HashMap<String, String> requestBody = new HashMap<>(); |
|
72 |
|
|
73 |
requestBody.put("name", "userName"); |
|
74 |
requestBody.put("token", token); |
|
75 | 71 |
|
76 | 72 |
return RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, token); |
77 | 73 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java | ||
---|---|---|
26 | 26 |
return restTemplate.postForEntity(url, entity, String.class); |
27 | 27 |
} |
28 | 28 |
|
29 |
public static ResponseEntity<String> sendRequestResponse(String url, HashMap<String,String> body, String token) { |
|
30 |
RestTemplate restTemplate = new RestTemplate(); |
|
31 |
String json = JSONBuilder.buildJson(body); |
|
32 |
|
|
33 |
HttpHeaders headers = new HttpHeaders(); |
|
34 |
|
|
35 |
headers.setContentType(MediaType.APPLICATION_JSON); |
|
36 |
headers.set("Authorization", "Bearer " + token); |
|
37 |
|
|
38 |
HttpEntity<String> entity = new HttpEntity<>(json, headers); |
|
39 |
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); |
|
40 |
return response; |
|
41 |
} |
|
42 |
|
|
43 | 29 |
public static ResponseEntity<String> sendRequestResponse(String url, String token) { |
44 | 30 |
RestTemplate restTemplate = new RestTemplate(); |
45 | 31 |
|
... | ... | |
49 | 35 |
headers.set("Authorization", "Bearer " + token); |
50 | 36 |
|
51 | 37 |
HttpEntity<String> entity = new HttpEntity<>(null, headers); |
52 |
ResponseEntity<String> response = restTemplate.postForEntity("{}", entity, String.class);
|
|
38 |
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
|
|
53 | 39 |
return response; |
54 | 40 |
} |
55 | 41 |
|
Také k dispozici: Unified diff
#10244 implementace komunikace s autentizacni aplikaci.