4 |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder;
|
5 |
5 |
import org.springframework.beans.factory.annotation.Autowired;
|
6 |
6 |
import org.springframework.beans.factory.annotation.Value;
|
7 |
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
8 |
|
import org.springframework.http.HttpEntity;
|
9 |
|
import org.springframework.http.HttpHeaders;
|
10 |
|
import org.springframework.http.HttpMethod;
|
11 |
7 |
import org.springframework.http.ResponseEntity;
|
12 |
|
import org.springframework.security.authentication.BadCredentialsException;
|
13 |
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
14 |
|
import org.springframework.security.core.Authentication;
|
15 |
8 |
import org.springframework.security.core.AuthenticationException;
|
16 |
9 |
import org.springframework.security.core.userdetails.UserDetails;
|
17 |
10 |
import org.springframework.security.core.userdetails.UserDetailsService;
|
18 |
11 |
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
12 |
import org.springframework.security.web.AuthenticationEntryPoint;
|
19 |
13 |
import org.springframework.stereotype.Service;
|
20 |
|
import org.springframework.web.client.RestTemplate;
|
21 |
14 |
|
|
15 |
import javax.servlet.ServletException;
|
22 |
16 |
import javax.servlet.http.HttpServletRequest;
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
|
18 |
import java.io.IOException;
|
23 |
19 |
import java.util.ArrayList;
|
24 |
20 |
import java.util.HashMap;
|
25 |
21 |
|
... | ... | |
27 |
23 |
* Service which communicate with OAuth application
|
28 |
24 |
*/
|
29 |
25 |
@Service
|
30 |
|
public class OAuthServiceImpl implements OAuthService, UserDetailsService {
|
|
26 |
public class OAuthServiceImpl implements OAuthService, UserDetailsService, AuthenticationEntryPoint {
|
31 |
27 |
|
32 |
28 |
/**
|
33 |
29 |
* URL path to authenticate endpoint of OAuth application
|
... | ... | |
44 |
40 |
/**
|
45 |
41 |
* URL path to logout endpoint of OAuth application
|
46 |
42 |
*/
|
47 |
|
@Value("${auth.realm.logout}")
|
48 |
|
private String AUTH_URL_LOGOUT;
|
|
43 |
// @Value("${auth.realm.logout}")
|
|
44 |
private String AUTH_URL_LOGOUT = "http://localhost:8081/logout";
|
49 |
45 |
|
50 |
46 |
/**
|
51 |
47 |
*
|
... | ... | |
53 |
49 |
@Autowired
|
54 |
50 |
private UserService userService;
|
55 |
51 |
|
|
52 |
@Override
|
|
53 |
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
|
|
54 |
throws IOException, ServletException {
|
|
55 |
String authorizationHeader = request.getHeader("Authorization");
|
|
56 |
if (authorizationHeader == null || authorizationHeader.length() < 7) {
|
|
57 |
return;
|
|
58 |
}
|
|
59 |
String token = authorizationHeader.substring(7);
|
|
60 |
|
|
61 |
ResponseEntity<String> responseEntity = authenticate(token);
|
|
62 |
if (token != null && responseEntity.getBody().contains("OK")) {
|
|
63 |
// Token is valid, proceed with the request
|
|
64 |
response.setStatus(501);
|
|
65 |
} else {
|
|
66 |
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
56 |
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);
|
57 |
75 |
|
58 |
|
return RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, null, token);
|
|
76 |
return RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, requestBody);
|
59 |
77 |
}
|
60 |
78 |
|
61 |
79 |
public ResponseEntity<String> loginUser(User user) {
|
#10228 authentication entry point