Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
carregando urls e roles dinamicamente
- Loading branch information
Showing
7 changed files
with
210 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/br/com/casadocodigo/loja/conf/DynamicSecurityMetadataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package br.com.casadocodigo.loja.conf; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.access.ConfigAttribute; | ||
import org.springframework.security.web.FilterInvocation; | ||
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import br.com.casadocodigo.loja.daos.SystemURLDAO; | ||
import br.com.casadocodigo.loja.models.Role; | ||
import br.com.casadocodigo.loja.models.SystemURL; | ||
|
||
@Component | ||
public class DynamicSecurityMetadataSource implements | ||
FilterInvocationSecurityMetadataSource { | ||
|
||
@Autowired | ||
private SystemURLDAO systemUrls;; | ||
|
||
@Override | ||
public Collection<ConfigAttribute> getAttributes(Object object) | ||
throws IllegalArgumentException { | ||
final HttpServletRequest request = ((FilterInvocation) object) | ||
.getRequest(); | ||
|
||
String urlWithoutContextPath = request.getRequestURI().substring( | ||
request.getContextPath().length()); | ||
|
||
Optional<SystemURL> foundUrl = systemUrls | ||
.findByURL(urlWithoutContextPath); | ||
|
||
if (foundUrl.isPresent()) { | ||
System.out.println("achou a url"); | ||
return foundUrl.get().getRolesAllowed().stream() | ||
.map(this::configAttribute).collect(Collectors.toList()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private ConfigAttribute configAttribute(Role role) { | ||
return new ConfigAttribute() { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = -474661209383691172L; | ||
|
||
@Override | ||
public String getAttribute() { | ||
return role.getAuthority(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Collection<ConfigAttribute> getAllConfigAttributes() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> clazz) { | ||
return FilterInvocation.class.isAssignableFrom(clazz); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String test = "/casadocodigo-blog/produtos/form"; | ||
System.out.println(test.substring("/casadocodigo-blog".length())); | ||
} | ||
|
||
} |
27 changes: 23 additions & 4 deletions
27
src/main/java/br/com/casadocodigo/loja/conf/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/br/com/casadocodigo/loja/controllers/SystemURLsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package br.com.casadocodigo.loja.controllers; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import javax.transaction.Transactional; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import br.com.casadocodigo.loja.models.Role; | ||
import br.com.casadocodigo.loja.models.SystemURL; | ||
|
||
/** | ||
* Apenas para a ajudar a criar algumas urls associadas a roles | ||
* @author alberto | ||
* | ||
*/ | ||
@Controller | ||
public class SystemURLsController { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
@RequestMapping("/url") | ||
@ResponseBody | ||
@Transactional | ||
public String create(String url,String roleName) { | ||
SystemURL systemURL = new SystemURL(); | ||
systemURL.setValue(url); | ||
systemURL.setRolesAllowed(Arrays.asList(new Role(roleName))); | ||
em.persist(systemURL); | ||
return "urls criadas"; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/br/com/casadocodigo/loja/daos/SystemURLDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package br.com.casadocodigo.loja.daos; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.TypedQuery; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import br.com.casadocodigo.loja.models.SystemURL; | ||
|
||
@Component | ||
public class SystemURLDAO { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
public Optional<SystemURL> findByURL(String url) { | ||
TypedQuery<SystemURL> query = entityManager | ||
.createQuery("select su from SystemURL su join fetch su.rolesAllowed where su.value = :url",SystemURL.class); | ||
query.setParameter("url", url); | ||
List<SystemURL> urls = query.getResultList(); | ||
if(urls.isEmpty()){ | ||
return Optional.empty(); | ||
} | ||
return Optional.of(urls.get(0)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/br/com/casadocodigo/loja/models/SystemURL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package br.com.casadocodigo.loja.models; | ||
|
||
import java.util.List; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToMany; | ||
|
||
@Entity | ||
public class SystemURL { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
@Column(unique=true) | ||
private String value; | ||
@ManyToMany | ||
private List<Role> rolesAllowed; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
public List<Role> getRolesAllowed() { | ||
return rolesAllowed; | ||
} | ||
|
||
public void setRolesAllowed(List<Role> rolesAllowed) { | ||
this.rolesAllowed = rolesAllowed; | ||
} | ||
|
||
} |