Projekt

Obecné

Profil

Stáhnout (5.43 KB) Statistiky
| Větev: | Tag: | Revize:
1
package cz.zcu.kiv.backendapi.security;
2

    
3
import cz.zcu.kiv.backendapi.security.jwt.JwtTokenVerifier;
4
import cz.zcu.kiv.backendapi.security.jwt.JwtUsernameAndPasswordAuthenticationFilter;
5
import cz.zcu.kiv.backendapi.security.jwt.JwtUtils;
6
import cz.zcu.kiv.backendapi.user.Role;
7
import cz.zcu.kiv.backendapi.user.permission.Permission;
8
import lombok.RequiredArgsConstructor;
9
import org.springframework.context.annotation.Bean;
10
import org.springframework.context.annotation.Configuration;
11
import org.springframework.http.HttpMethod;
12
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
13
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
14
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
15
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
17
import org.springframework.security.config.http.SessionCreationPolicy;
18
import org.springframework.security.core.userdetails.UserDetailsService;
19
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
20
import org.springframework.web.cors.CorsConfiguration;
21
import org.springframework.web.cors.CorsConfigurationSource;
22
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
23

    
24
import java.util.HashMap;
25
import java.util.Map;
26

    
27
/**
28
 * Security config class
29
 */
30
@Configuration
31
@EnableWebSecurity
32
@RequiredArgsConstructor
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

    
40
    /**
41
     * User detail service
42
     */
43
    private final UserDetailsService userDetailsService;
44

    
45
    /**
46
     * Password encoder
47
     */
48
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
49

    
50
    /**
51
     * JWT utils
52
     */
53
    private final JwtUtils jwtUtils;
54

    
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
        PERMITTED_ENDPOINTS.put("/title-page", HttpMethod.GET);
66
    }
67

    
68
    /**
69
     * Security configuration
70
     *
71
     * @param http http security
72
     * @throws Exception exception
73
     */
74
    // TODO configure and check rights
75
    @Override
76
    protected void configure(HttpSecurity http) throws Exception {
77
        http.csrf().disable()
78
                .cors()
79
                .and()
80
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
81
                .and()
82
                .addFilter(new JwtUsernameAndPasswordAuthenticationFilter(authenticationManager(), jwtUtils))
83
                .addFilterAfter(new JwtTokenVerifier(jwtUtils, PERMITTED_ENDPOINTS), JwtUsernameAndPasswordAuthenticationFilter.class)
84
                .authorizeRequests()
85
                .antMatchers(HttpMethod.GET, PERMITTED_ENDPOINTS.keySet().stream().filter(k -> PERMITTED_ENDPOINTS.get(k).equals(HttpMethod.GET)).toArray(String[]::new)).permitAll()
86
                .antMatchers(HttpMethod.POST, "/login").permitAll()
87
                .antMatchers("/external-catalog-items").hasRole(Role.ADMIN.name())
88
                .antMatchers(HttpMethod.PATCH, "/users/*/permissions", "/users/*/password").hasRole(Role.ADMIN.name())
89
                .antMatchers(HttpMethod.DELETE, "/users/**").hasRole(Role.ADMIN.name())
90
                .antMatchers(HttpMethod.GET, "/users").hasRole(Role.ADMIN.name())
91
                .antMatchers(HttpMethod.GET, "/path").hasAuthority(Permission.READ.name())
92
                .antMatchers(HttpMethod.POST, "/catalog-items").hasAuthority(Permission.WRITE.name())
93
                .antMatchers(HttpMethod.PUT, "/catalog-items/*").hasAuthority(Permission.WRITE.name())
94
                .antMatchers(HttpMethod.DELETE, "/catalog-items/*").hasAuthority(Permission.DELETE.name())
95
                .anyRequest()
96
                .authenticated();
97
    }
98

    
99
    /**
100
     * Sets authentication provider to authentication manager
101
     *
102
     * @param auth authentication manager builder
103
     */
104
    @Override
105
    protected void configure(final AuthenticationManagerBuilder auth) {
106
        auth.authenticationProvider(authenticationProvider());
107
    }
108

    
109

    
110
    /**
111
     * Returns authentication provider
112
     *
113
     * @return authentication provider
114
     */
115
    @Bean
116
    public DaoAuthenticationProvider authenticationProvider() {
117
        final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
118
        provider.setUserDetailsService(userDetailsService);
119
        provider.setPasswordEncoder(bCryptPasswordEncoder);
120
        return provider;
121
    }
122

    
123
    @Bean
124
    CorsConfigurationSource corsConfigurationSource() {
125
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
126
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
127
        return source;
128
    }
129
}
(2-2/2)