Skip to content

Commit 97272b0

Browse files
committed
Autorizando endpoints para perfis específicos
1 parent 75a3096 commit 97272b0

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.core.env.Environment;
99
import org.springframework.http.HttpMethod;
1010
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
11+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1112
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1213
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1314
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -24,6 +25,7 @@
2425

2526
@Configuration
2627
@EnableWebSecurity
28+
@EnableGlobalMethodSecurity(prePostEnabled = true)
2729
public class SecurityConfig extends WebSecurityConfigurerAdapter {
2830

2931
@Autowired
@@ -41,7 +43,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
4143

4244
private static final String[] PUBLIC_MATCHERS_GET = {
4345
"/produtos/**",
44-
"/categorias/**",
46+
"/categorias/**"
47+
};
48+
49+
private static final String[] PUBLIC_MATCHERS_POST = {
4550
"/clientes/**"
4651
};
4752

@@ -54,6 +59,7 @@ protected void configure(HttpSecurity http) throws Exception {
5459

5560
http.cors().and().csrf().disable();
5661
http.authorizeRequests()
62+
.antMatchers(HttpMethod.POST, PUBLIC_MATCHERS_POST).permitAll()
5763
.antMatchers(HttpMethod.GET, PUBLIC_MATCHERS_GET).permitAll()
5864
.antMatchers(PUBLIC_MATCHERS).permitAll()
5965
.anyRequest().authenticated();

src/main/java/jdc/loja/resources/CategoriaResource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.data.domain.Page;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.security.access.prepost.PreAuthorize;
1213
import org.springframework.web.bind.annotation.PathVariable;
1314
import org.springframework.web.bind.annotation.RequestBody;
1415
import org.springframework.web.bind.annotation.RequestMapping;
@@ -34,6 +35,7 @@ public ResponseEntity<Categoria> find(@PathVariable Integer id) {
3435
return ResponseEntity.ok().body(obj);
3536
}
3637

38+
@PreAuthorize("hasAnyRole('ADMIN')")
3739
@RequestMapping(method=RequestMethod.POST)
3840
public ResponseEntity<Void> insert(@Valid @RequestBody CategoriaDTO objDto) {
3941
Categoria obj = service.fromDTO(objDto);
@@ -43,6 +45,7 @@ public ResponseEntity<Void> insert(@Valid @RequestBody CategoriaDTO objDto) {
4345
return ResponseEntity.created(uri).build();
4446
}
4547

48+
@PreAuthorize("hasAnyRole('ADMIN')")
4649
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
4750
public ResponseEntity<Void> update(@Valid @RequestBody CategoriaDTO objDto,
4851
@PathVariable Integer id) {
@@ -52,6 +55,7 @@ public ResponseEntity<Void> update(@Valid @RequestBody CategoriaDTO objDto,
5255
return ResponseEntity.noContent().build();
5356
}
5457

58+
@PreAuthorize("hasAnyRole('ADMIN')")
5559
@RequestMapping(value="{id}", method=RequestMethod.DELETE)
5660
public ResponseEntity<Void> delete(@PathVariable Integer id) {
5761
service.delete(id);

src/main/java/jdc/loja/resources/ClienteResource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.data.domain.Page;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.security.access.prepost.PreAuthorize;
1213
import org.springframework.transaction.annotation.Transactional;
1314
import org.springframework.web.bind.annotation.PathVariable;
1415
import org.springframework.web.bind.annotation.RequestBody;
@@ -55,12 +56,14 @@ public ResponseEntity<Void> update(@Valid @RequestBody ClienteDTO objDto,
5556
return ResponseEntity.noContent().build();
5657
}
5758

59+
@PreAuthorize("hasAnyRole('ADMIN')")
5860
@RequestMapping(value="{id}", method=RequestMethod.DELETE)
5961
public ResponseEntity<Void> delete(@PathVariable Integer id) {
6062
service.delete(id);
6163
return ResponseEntity.noContent().build();
6264
}
6365

66+
@PreAuthorize("hasAnyRole('ADMIN')")
6467
@RequestMapping(method=RequestMethod.GET)
6568
public ResponseEntity<List<ClienteDTO>> findAll() {
6669
List<Cliente> list = service.findAll();
@@ -69,6 +72,7 @@ public ResponseEntity<List<ClienteDTO>> findAll() {
6972
return ResponseEntity.ok().body(listDto);
7073
}
7174

75+
@PreAuthorize("hasAnyRole('ADMIN')")
7276
@RequestMapping(value="/page", method=RequestMethod.GET)
7377
public ResponseEntity<Page<ClienteDTO>> findPage(
7478
@RequestParam(value="page", defaultValue="0") Integer page,

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ default.sender=email
44
default.recipient=email
55

66
jwt.secret=g.Op)-0h*hN2-@Vs2*Sf,29xtz[*]~s
7-
jwt.expiration=60000
7+
jwt.expiration=120000

0 commit comments

Comments
 (0)