Revize a47418af
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
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 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider; |
|
3 | 4 |
import org.springframework.beans.factory.annotation.Autowired; |
4 | 5 |
import org.springframework.context.annotation.Bean; |
5 | 6 |
import org.springframework.security.authentication.AuthenticationManager; |
... | ... | |
9 | 10 |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
10 | 11 |
import org.springframework.security.config.http.SessionCreationPolicy; |
11 | 12 |
import org.springframework.security.core.userdetails.UserDetailsService; |
13 |
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
|
14 |
import org.springframework.web.client.RestTemplate; |
|
12 | 15 |
|
13 | 16 |
@EnableWebSecurity |
14 | 17 |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
... | ... | |
21 | 24 |
auth.userDetailsService(userService); |
22 | 25 |
} |
23 | 26 |
|
27 |
@Bean |
|
28 |
public AuthenticationProvider provider() { |
|
29 |
return new AuthProvider(template()); |
|
30 |
} |
|
31 |
|
|
32 |
@Bean |
|
33 |
public RestTemplate template() { |
|
34 |
return new RestTemplate(); |
|
35 |
} |
|
24 | 36 |
|
25 | 37 |
@Bean |
26 | 38 |
public UserDetailsService userDetailsService() { |
27 | 39 |
return super.userDetailsService(); |
28 | 40 |
} |
29 | 41 |
|
42 |
|
|
43 |
|
|
30 | 44 |
@Override |
31 | 45 |
@Bean |
32 | 46 |
public AuthenticationManager authenticationManagerBean() throws Exception { |
33 | 47 |
return super.authenticationManagerBean(); |
34 | 48 |
} |
35 | 49 |
|
50 |
|
|
36 | 51 |
@Override |
37 | 52 |
protected void configure(HttpSecurity httpSecurity) throws Exception { |
38 |
httpSecurity.csrf().disable() |
|
39 |
.authorizeRequests().antMatchers("/v2/user/register","/v2/user/login").permitAll(). |
|
40 |
anyRequest().authenticated().and(). |
|
41 |
exceptionHandling().and().sessionManagement() |
|
53 |
httpSecurity |
|
54 |
.csrf() |
|
55 |
.disable() |
|
56 |
.authorizeRequests() |
|
57 |
.antMatchers("/v2/user/register", "/v2/user/login").permitAll() |
|
58 |
.anyRequest() |
|
59 |
.authenticated() |
|
60 |
.and() |
|
61 |
.sessionManagement() |
|
42 | 62 |
.sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
43 |
// httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class); |
|
44 |
|
|
63 |
httpSecurity.addFilterAfter(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); |
|
45 | 64 |
} |
46 | 65 |
|
47 |
|
|
48 |
|
|
49 | 66 |
} |
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; |
|
7 | 11 |
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 |
import org.springframework.security.core.AuthenticationException; |
|
8 | 16 |
import org.springframework.security.core.userdetails.UserDetails; |
9 | 17 |
import org.springframework.security.core.userdetails.UserDetailsService; |
10 | 18 |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
11 | 19 |
import org.springframework.stereotype.Service; |
20 |
import org.springframework.web.client.RestTemplate; |
|
21 |
|
|
22 |
import javax.servlet.http.HttpServletRequest; |
|
12 | 23 |
import java.util.ArrayList; |
13 | 24 |
import java.util.HashMap; |
14 | 25 |
|
... | ... | |
25 | 36 |
@Autowired |
26 | 37 |
private UserService userService; |
27 | 38 |
|
28 |
public ResponseEntity<String> authenticate(User user) { |
|
29 |
final String userName = user.getName(); |
|
30 |
final String token = user.getToken(); |
|
31 |
|
|
32 |
if(userName == null || token == null) { |
|
33 |
return null; |
|
34 |
} |
|
35 |
|
|
36 |
//HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL); |
|
37 |
HashMap<String, String> requestBody = new HashMap<>(); |
|
39 |
public ResponseEntity<String> authenticate(String token) { |
|
38 | 40 |
|
39 | 41 |
requestBody.put("name", userName); |
40 | 42 |
requestBody.put("token", token); |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/JSONBuilder.java | ||
---|---|---|
17 | 17 |
* @return String representation of JSON object |
18 | 18 |
*/ |
19 | 19 |
public static String buildJson(HashMap<String, String> map){ |
20 |
if(map == null) { |
|
21 |
return ""; |
|
22 |
} |
|
20 | 23 |
ObjectMapper mapper = new ObjectMapper(); |
21 | 24 |
ObjectNode jsonObject = mapper.createObjectNode(); |
22 | 25 |
for (String key : map.keySet()) { |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java | ||
---|---|---|
3 | 3 |
import org.springframework.web.client.RestTemplate; |
4 | 4 |
|
5 | 5 |
import java.util.HashMap; |
6 |
import java.util.Optional; |
|
6 | 7 |
import java.util.logging.Logger; |
7 | 8 |
|
8 | 9 |
import org.springframework.http.*; |
... | ... | |
25 | 26 |
return response; |
26 | 27 |
} |
27 | 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.setBearerAuth(token); |
|
37 |
|
|
38 |
HttpEntity<String> entity = new HttpEntity<>(json, headers); |
|
39 |
return restTemplate.postForEntity(url, entity, String.class); |
|
40 |
} |
|
41 |
|
|
28 | 42 |
} |
Také k dispozici: Unified diff
#10228 Implementování odhlášení uživatele a invalidace tokenu