SecurityConfigurations.java
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.srh.api.security;
import com.srh.api.repository.ApiUserRepository;
import com.srh.api.service.AuthService;
import com.srh.api.service.JWTService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@EnableWebSecurity
@Configuration
public class SecurityConfigurations extends WebSecurityConfigurerAdapter {
@Autowired
private AuthService authService;
@Autowired
private JWTService jwtService;
@Autowired
private ApiUserRepository APIUserRepository;
@Override
@SneakyThrows
@Bean
protected AuthenticationManager authenticationManager() {
return super.authenticationManager();
}
@Override
@SneakyThrows
protected void configure(AuthenticationManagerBuilder auth) {
auth.userDetailsService(authService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
@SneakyThrows
protected void configure(HttpSecurity http) {
// http.authorizeRequests()
// .antMatchers(HttpMethod.POST, "/auth").permitAll()
// .antMatchers("/h2-console/**").permitAll()
// .antMatchers("/itens").hasRole("ADMIN")
// .antMatchers("/projects").hasRole("ADMIN")
// .antMatchers("/users/admins").hasRole("ADMIN")
// .antMatchers("/users/apis").hasRole("ADMIN")
// .antMatchers("/recommendations/types").hasRole("ADMIN")
// .anyRequest().hasRole("USER")
// .and().cors()
// .and().csrf().disable()
// .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
// .and().addFilterBefore(new AuthenticationWithTokenFilter(jwtService, APIUserRepository),
// UsernamePasswordAuthenticationFilter.class);
http.authorizeRequests()
.antMatchers("/**").permitAll()
.and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().addFilterBefore(new AuthenticationWithTokenFilter(jwtService, APIUserRepository),
UsernamePasswordAuthenticationFilter.class);
http.headers().frameOptions().disable();
}
@Override
@SneakyThrows
public void configure(WebSecurity web) {
web.ignoring()
.antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");
}
}