Revize 1d732187
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService; |
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.core.annotation.Order; |
|
9 |
import org.springframework.http.ResponseEntity; |
|
10 |
import org.springframework.security.authentication.AuthenticationProvider; |
|
11 |
import org.springframework.stereotype.Component; |
|
12 |
import org.springframework.web.filter.OncePerRequestFilter; |
|
13 |
|
|
14 |
import javax.servlet.FilterChain; |
|
15 |
import javax.servlet.ServletException; |
|
16 |
import javax.servlet.http.HttpServletRequest; |
|
17 |
import javax.servlet.http.HttpServletResponse; |
|
18 |
import java.io.BufferedReader; |
|
19 |
import java.io.BufferedWriter; |
|
20 |
import java.io.IOException; |
|
21 |
import java.util.Objects; |
|
22 |
|
|
23 |
@Component |
|
24 |
public class JwtAuthenticationFilter extends OncePerRequestFilter { |
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
@Override |
|
29 |
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
|
30 |
System.out.println("------------------DO FILTER INTERNAL---------------------------"); |
|
31 |
|
|
32 |
String authorizationHeader = request.getHeader("Authorization"); |
|
33 |
if(authorizationHeader == null || authorizationHeader.length() < 7) { |
|
34 |
filterChain.doFilter(request, response); |
|
35 |
return; |
|
36 |
} |
|
37 |
OAuthServiceImpl oAuthService = new OAuthServiceImpl(); |
|
38 |
|
|
39 |
String token = authorizationHeader.substring(7); |
|
40 |
|
|
41 |
ResponseEntity<String> oAuthResponse = oAuthService.authenticate(token); |
|
42 |
response.setContentType("application/json"); |
|
43 |
response.setStatus(oAuthResponse.getStatusCodeValue()); |
|
44 |
|
|
45 |
BufferedWriter out = new BufferedWriter(response.getWriter()); |
|
46 |
out.write(Objects.requireNonNull(oAuthResponse.getBody())); |
|
47 |
out.flush(); |
|
48 |
out.close(); |
|
49 |
|
|
50 |
filterChain.doFilter(request, response); |
|
51 |
|
|
52 |
} |
|
53 |
|
|
54 |
|
|
55 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AuthProvider.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import org.springframework.beans.factory.annotation.Autowired; |
|
4 |
import org.springframework.beans.factory.annotation.Value; |
|
5 |
import org.springframework.boot.web.client.RestTemplateBuilder; |
|
6 |
import org.springframework.http.HttpEntity; |
|
7 |
import org.springframework.http.HttpHeaders; |
|
8 |
import org.springframework.http.HttpMethod; |
|
9 |
import org.springframework.http.ResponseEntity; |
|
10 |
import org.springframework.security.authentication.AuthenticationProvider; |
|
11 |
import org.springframework.security.authentication.BadCredentialsException; |
|
12 |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
|
13 |
|
|
14 |
import org.springframework.security.core.Authentication; |
|
15 |
import org.springframework.security.core.AuthenticationException; |
|
16 |
import org.springframework.security.core.userdetails.UserDetails; |
|
17 |
import org.springframework.stereotype.Service; |
|
18 |
import org.springframework.web.client.RestTemplate; |
|
19 |
|
|
20 |
@Service |
|
21 |
public class AuthProvider implements AuthenticationProvider { |
|
22 |
|
|
23 |
@Value("${auth.realm.authenticate}") |
|
24 |
private String auth_url; |
|
25 |
|
|
26 |
private RestTemplate template; |
|
27 |
|
|
28 |
public AuthProvider(RestTemplate restTemplate){ |
|
29 |
this.template = restTemplate; |
|
30 |
} |
|
31 |
|
|
32 |
|
|
33 |
@Override |
|
34 |
public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
|
35 |
|
|
36 |
RestTemplate restTemplate = new RestTemplate(); |
|
37 |
String jwtToken = (String) authentication.getCredentials(); |
|
38 |
|
|
39 |
HttpHeaders headers = new HttpHeaders(); |
|
40 |
headers.setBearerAuth(jwtToken); |
|
41 |
HttpEntity<String> entity = new HttpEntity<>(headers); |
|
42 |
|
|
43 |
try { |
|
44 |
ResponseEntity<UserDetails> responseEntity = restTemplate.exchange( |
|
45 |
auth_url, |
|
46 |
HttpMethod.POST, |
|
47 |
entity, |
|
48 |
UserDetails.class); |
|
49 |
|
|
50 |
UserDetails userDetails = responseEntity.getBody(); |
|
51 |
// List<GrantedAuthority> authorities = new ArrayList<>(); |
|
52 |
// authorities.add(new SimpleGrantedAuthority(userDetails.getRole())); |
|
53 |
|
|
54 |
if (userDetails != null && userDetails.getUsername() != null) { |
|
55 |
return new UsernamePasswordAuthenticationToken(userDetails.getUsername(), jwtToken); |
|
56 |
} |
|
57 |
|
|
58 |
return new UsernamePasswordAuthenticationToken(null, null); |
|
59 |
|
|
60 |
} catch (Exception e) { |
|
61 |
throw new BadCredentialsException("Invalid JWT token"); |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public boolean supports(Class<?> authentication) { |
|
67 |
return authentication.equals(UsernamePasswordAuthenticationToken.class); |
|
68 |
} |
|
69 |
|
|
70 |
|
|
71 |
} |
Také k dispozici: Unified diff
#10228 Implementování odhlášení uživatele a invalidace tokenu