Skip to content

Commit c5eaceb

Browse files
Configurado o Jwt
1 parent 6570ddb commit c5eaceb

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

testeSantanderWay/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
<artifactId>spring-boot-starter-security</artifactId>
5454
</dependency>
5555

56+
<dependency>
57+
<groupId>io.jsonwebtoken</groupId>
58+
<artifactId>jjwt</artifactId>
59+
<version>0.9.1</version>
60+
</dependency>
5661

5762
<dependency>
5863
<groupId>org.springframework.boot</groupId>

testeSantanderWay/src/main/java/br/com/testesantanderway/config/security/ConfigSeguranca.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package br.com.testesantanderway.config.security;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.http.HttpMethod;
7+
import org.springframework.security.authentication.AuthenticationManager;
68
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
79
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
810
import org.springframework.security.config.annotation.web.builders.WebSecurity;
911
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1012
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
13+
import org.springframework.security.config.http.SessionCreationPolicy;
1114
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1215

1316
@EnableWebSecurity
@@ -16,6 +19,12 @@ public class ConfigSeguranca extends WebSecurityConfigurerAdapter {
1619
@Autowired
1720
private AutenticacaoService autenticacaoService;
1821

22+
@Override
23+
@Bean
24+
protected AuthenticationManager authenticationManager() throws Exception{
25+
return super.authenticationManager();
26+
}
27+
1928
//Autenticação
2029
@Override
2130
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@@ -28,8 +37,14 @@ protected void configure(HttpSecurity http) throws Exception {
2837
http.authorizeRequests()
2938
.antMatchers(HttpMethod.GET,"/clientes").permitAll()
3039
.antMatchers(HttpMethod.GET,"/clientes/*").permitAll()
40+
.antMatchers(HttpMethod.POST,"/auth").permitAll()
41+
.antMatchers(
42+
"/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**",
43+
"/swagger.json")
44+
.permitAll()
3145
.anyRequest().authenticated()
32-
.and().formLogin();
46+
.and().csrf().disable()
47+
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
3348
}
3449

3550
//Recursos estáticos(js, css, img, etc.)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package br.com.testesantanderway.config.security;
2+
3+
import br.com.testesantanderway.modelo.Cliente;
4+
import io.jsonwebtoken.Jwts;
5+
import io.jsonwebtoken.SignatureAlgorithm;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.Date;
11+
12+
@Service
13+
public class ServicoDeToken {
14+
@Value("${testeSantanderWay.jwt.expiration}")
15+
private String expiracao;
16+
@Value("${testeSantanderWay.jwt.secret}")
17+
private String secret;
18+
19+
public String gerarToken(Authentication authentication) {
20+
Cliente clienteLogado = (Cliente) authentication.getPrincipal();
21+
Date hoje = new Date();
22+
Date dataExpiracao = new Date(hoje.getTime() + Long.parseLong(expiracao));
23+
24+
return Jwts.builder()
25+
.setIssuer("Api teste way")
26+
.setSubject(clienteLogado.getCodigoUsuario())
27+
.setIssuedAt(hoje)
28+
.setExpiration(dataExpiracao)
29+
.signWith(SignatureAlgorithm.HS256, secret).compact();
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package br.com.testesantanderway.controller;
2+
3+
import br.com.testesantanderway.config.security.ServicoDeToken;
4+
import br.com.testesantanderway.controller.form.AuthForm;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.security.authentication.AuthenticationManager;
8+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
9+
import org.springframework.security.core.Authentication;
10+
import org.springframework.security.core.AuthenticationException;
11+
import org.springframework.security.core.token.TokenService;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import javax.validation.Valid;
18+
19+
@RestController
20+
@RequestMapping("/auth")
21+
public class AuthController {
22+
@Autowired
23+
private AuthenticationManager authManager;
24+
@Autowired
25+
private ServicoDeToken servicoDeToken;
26+
27+
@PostMapping
28+
public ResponseEntity<?> login(@RequestBody AuthForm form){
29+
UsernamePasswordAuthenticationToken dadosLogin = form.converter();
30+
try {
31+
Authentication authentication = authManager.authenticate(dadosLogin);
32+
String token = servicoDeToken.gerarToken(authentication);
33+
System.out.println(token);
34+
return ResponseEntity.ok().build();
35+
} catch (AuthenticationException e){
36+
return ResponseEntity.badRequest().build();
37+
}
38+
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package br.com.testesantanderway.controller.form;
2+
3+
import org.hibernate.validator.constraints.Length;
4+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
5+
6+
import javax.validation.constraints.NotEmpty;
7+
import javax.validation.constraints.NotNull;
8+
9+
public class AuthForm {
10+
private String email;
11+
private String senha;
12+
13+
public String getEmail() {
14+
return email;
15+
}
16+
17+
public void setEmail(String email) {
18+
this.email = email;
19+
}
20+
21+
public String getSenha() {
22+
return senha;
23+
}
24+
25+
public void setSenha(String senha) {
26+
this.senha = senha;
27+
}
28+
29+
public UsernamePasswordAuthenticationToken converter() {
30+
return new UsernamePasswordAuthenticationToken(email, senha);
31+
}
32+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#Solr
2-
spring.data.solr.host=http://localhost:8983/solr
2+
spring.data.solr.host=http://localhost:8983/solr
3+
4+
#JWT
5+
testeSantanderWay.jwt.secret=A+X;fTJP&Pd,TD9dwVq(hsHX,ya^<wsD_UK7L+@=S;{'CydP]{v@}G'b>et;yz$*yL5S8EJN:%P:X%H9>#nYLrX}@s?CQcpspH,2emzBc!Q[V'AYa~uzF8WR~AUrMzxp/V$9([S9X#zj/CH('#]B_Hc+%fGhe27YB;^j4Xk=Ju"Ap~_&<L;=!Z;!,2UP;!hF3P]j85#*`&T]/kB/W^6$v~u6qpejL>kY^f)sy4:qTq_Ec!-z!@aAp~sLKGU>$
6+
testeSantanderWay.jwt.expiration=1800000

0 commit comments

Comments
 (0)