Projekt

Obecné

Profil

« Předchozí | Další » 

Revize df7a3957

Přidáno uživatelem Petr Urban před asi 2 roky(ů)

#10228 authentication entry point

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java
4 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider;
5 5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
6 6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
7 8
import org.springframework.beans.factory.annotation.Autowired;
8 9
import org.springframework.core.annotation.Order;
9 10
import org.springframework.http.*;
......
22 23
import java.io.BufferedReader;
23 24
import java.io.BufferedWriter;
24 25
import java.io.IOException;
26
import java.util.HashMap;
25 27
import java.util.Objects;
26 28

  
27 29
@Component
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security;
2 2

  
3 3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl;
4 6
import org.springframework.beans.factory.annotation.Autowired;
5 7
import org.springframework.boot.web.client.RestTemplateBuilder;
6 8
import org.springframework.context.annotation.Bean;
......
45 47
        return super.userDetailsService();
46 48
    }
47 49

  
48

  
50
//    @Bean
51
//    public JwtAuthenticationFilter jwtAuthenticationFilter(OAuthService oAuthService) {
52
//        return new JwtAuthenticationFilter(oAuthService);
53
//    }
49 54

  
50 55
    @Override
51 56
    @Bean
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java
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) {

Také k dispozici: Unified diff