Skip to content

Commit 6a73676

Browse files
committed
Configuração inicial do Spring Security
1 parent 1683673 commit 6a73676

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@
6767
<groupId>org.springframework.boot</groupId>
6868
<artifactId>spring-boot-starter-thymeleaf</artifactId>
6969
</dependency>
70+
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-security</artifactId>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>io.jsonwebtoken</groupId>
78+
<artifactId>jjwt</artifactId>
79+
<version>0.7.0</version>
80+
</dependency>
7081

7182
</dependencies>
7283

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package jdc.loja.config;
2+
3+
import java.util.Arrays;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.core.env.Environment;
9+
import org.springframework.http.HttpMethod;
10+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
11+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
12+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13+
import org.springframework.security.config.http.SessionCreationPolicy;
14+
import org.springframework.web.cors.CorsConfiguration;
15+
import org.springframework.web.cors.CorsConfigurationSource;
16+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
17+
18+
@Configuration
19+
@EnableWebSecurity
20+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
21+
22+
@Autowired
23+
private Environment env;
24+
25+
private static final String[] PUBLIC_MATCHERS = {
26+
"/h2-console/**"
27+
};
28+
29+
private static final String[] PUBLIC_MATCHERS_GET = {
30+
"/produtos/**",
31+
"/categorias/**"
32+
};
33+
34+
@Override
35+
protected void configure(HttpSecurity http) throws Exception {
36+
37+
if(Arrays.asList(env.getActiveProfiles()).contains("test")) {
38+
http.headers().frameOptions().disable();
39+
}
40+
41+
http.cors().and().csrf().disable();
42+
http.authorizeRequests()
43+
.antMatchers(HttpMethod.GET, PUBLIC_MATCHERS_GET).permitAll()
44+
.antMatchers(PUBLIC_MATCHERS).permitAll()
45+
.anyRequest().authenticated();
46+
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
47+
}
48+
49+
@Bean
50+
CorsConfigurationSource corsConfigurationSource() {
51+
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
52+
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
53+
return source;
54+
}
55+
}

src/main/java/jdc/loja/config/TestConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88
import org.springframework.context.annotation.Profile;
9+
import org.springframework.mail.javamail.JavaMailSenderImpl;
910

1011
import jdc.loja.services.DBService;
1112
import jdc.loja.services.EmailService;
@@ -28,4 +29,15 @@ public boolean instantiateDatabase() throws ParseException {
2829
public EmailService emailService() {
2930
return new MockEmailService();
3031
}
32+
33+
@Bean
34+
public JavaMailSenderImpl mailSender() {
35+
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
36+
37+
javaMailSender.setProtocol("SMTP");
38+
javaMailSender.setHost("127.0.0.1");
39+
javaMailSender.setPort(25);
40+
41+
return javaMailSender;
42+
}
3143
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spring.profiles.active=dev
1+
spring.profiles.active=test
22

33
default.sender=email
44
default.recipient=email

0 commit comments

Comments
 (0)