21 |
21 |
import org.springframework.web.cors.CorsConfigurationSource;
|
22 |
22 |
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
23 |
23 |
|
|
24 |
import java.util.HashMap;
|
|
25 |
import java.util.Map;
|
|
26 |
|
24 |
27 |
/**
|
25 |
28 |
* Security config class
|
26 |
29 |
*/
|
... | ... | |
28 |
31 |
@EnableWebSecurity
|
29 |
32 |
@RequiredArgsConstructor
|
30 |
33 |
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Map of permitted endpoints with HTTP method (user does not need to be authenticated perform the request)
|
|
37 |
*/
|
|
38 |
private static final Map<String, HttpMethod> PERMITTED_ENDPOINTS;
|
|
39 |
|
31 |
40 |
/**
|
32 |
41 |
* User detail service
|
33 |
42 |
*/
|
... | ... | |
43 |
52 |
*/
|
44 |
53 |
private final JwtUtils jwtUtils;
|
45 |
54 |
|
46 |
|
/**
|
47 |
|
* List of permitted pages without login
|
48 |
|
*/
|
49 |
|
private final String[] permittedUrls = new String[]{"/login", "/users/token", "/swagger-ui/**",
|
50 |
|
"/swagger-ui.html", "/v3/api-docs", "/v3/api-docs/swagger-config"};
|
|
55 |
static {
|
|
56 |
PERMITTED_ENDPOINTS = new HashMap<>();
|
|
57 |
PERMITTED_ENDPOINTS.put("/login", HttpMethod.POST);
|
|
58 |
PERMITTED_ENDPOINTS.put("/users/token", HttpMethod.GET);
|
|
59 |
PERMITTED_ENDPOINTS.put("/swagger-ui/**", HttpMethod.GET);
|
|
60 |
PERMITTED_ENDPOINTS.put("/swagger-ui.html", HttpMethod.GET);
|
|
61 |
PERMITTED_ENDPOINTS.put("/v3/api-docs", HttpMethod.GET);
|
|
62 |
PERMITTED_ENDPOINTS.put("/v3/api-docs/swagger-config", HttpMethod.GET);
|
|
63 |
PERMITTED_ENDPOINTS.put("/catalog-items", HttpMethod.GET);
|
|
64 |
PERMITTED_ENDPOINTS.put("/catalog-items/**", HttpMethod.GET);
|
|
65 |
}
|
51 |
66 |
|
52 |
67 |
/**
|
53 |
68 |
* Security configuration
|
... | ... | |
55 |
70 |
* @param http http security
|
56 |
71 |
* @throws Exception exception
|
57 |
72 |
*/
|
|
73 |
// TODO configure and check rights
|
58 |
74 |
@Override
|
59 |
75 |
protected void configure(HttpSecurity http) throws Exception {
|
60 |
76 |
http.csrf().disable()
|
... | ... | |
63 |
79 |
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
64 |
80 |
.and()
|
65 |
81 |
.addFilter(new JwtUsernameAndPasswordAuthenticationFilter(authenticationManager(), jwtUtils))
|
66 |
|
.addFilterAfter(new JwtTokenVerifier(jwtUtils, permittedUrls), JwtUsernameAndPasswordAuthenticationFilter.class)
|
|
82 |
.addFilterAfter(new JwtTokenVerifier(jwtUtils, PERMITTED_ENDPOINTS), JwtUsernameAndPasswordAuthenticationFilter.class)
|
67 |
83 |
.authorizeRequests()
|
68 |
|
.antMatchers(permittedUrls).permitAll()
|
69 |
|
.antMatchers("/user/update-permissions/**", "/user/reset-password/**").hasRole(Role.ADMIN.name())
|
70 |
|
.antMatchers(HttpMethod.DELETE, "/user/**").hasRole(Role.ADMIN.name())
|
71 |
|
.antMatchers("/write/**").hasAuthority(Permission.WRITE.name())
|
72 |
|
.antMatchers("/read/**").hasAuthority(Permission.READ.name())
|
73 |
|
.antMatchers("/delete/**").hasAuthority(Permission.DELETE.name())
|
|
84 |
.antMatchers(HttpMethod.GET, PERMITTED_ENDPOINTS.keySet().stream().filter(k -> PERMITTED_ENDPOINTS.get(k).equals(HttpMethod.GET)).toArray(String[]::new)).permitAll()
|
|
85 |
.antMatchers(HttpMethod.POST, "/login").permitAll()
|
|
86 |
.antMatchers(HttpMethod.PATCH, "/users/*/permissions", "/users/*/password").hasRole(Role.ADMIN.name())
|
|
87 |
.antMatchers(HttpMethod.DELETE, "/users/**").hasRole(Role.ADMIN.name())
|
|
88 |
.antMatchers(HttpMethod.GET, "/users").hasRole(Role.ADMIN.name())
|
|
89 |
.antMatchers(HttpMethod.GET, "/path").hasAuthority(Permission.READ.name())
|
|
90 |
.antMatchers(HttpMethod.POST, "/catalog-items").hasAuthority(Permission.WRITE.name())
|
|
91 |
.antMatchers(HttpMethod.PUT, "/catalog-items/*").hasAuthority(Permission.WRITE.name())
|
|
92 |
.antMatchers(HttpMethod.DELETE, "/catalog-items/*").hasAuthority(Permission.DELETE.name())
|
74 |
93 |
.anyRequest()
|
75 |
94 |
.authenticated();
|
76 |
95 |
}
|
Checked, added and fixed rights to endpoints
re #9511