mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 15:55:49 +00:00
modify role
This commit is contained in:
parent
d6ddf2c216
commit
bb5391bca9
12 changed files with 92 additions and 78 deletions
|
@ -18,17 +18,14 @@ import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||||
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@Override
|
* @Override public void registerStompEndpoints(StompEndpointRegistry
|
||||||
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
|
* stompEndpointRegistry) {
|
||||||
stompEndpointRegistry.addEndpoint("/websocket").withSockJS();
|
* stompEndpointRegistry.addEndpoint("/websocket").withSockJS(); }
|
||||||
}
|
*
|
||||||
|
* @Override public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||||
@Override
|
* registry.enableSimpleBroker("/topic");
|
||||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
* registry.setApplicationDestinationPrefixes("/app"); }
|
||||||
registry.enableSimpleBroker("/topic");
|
*
|
||||||
registry.setApplicationDestinationPrefixes("/app");
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||||
|
@ -38,22 +35,18 @@ public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||||
registry.addEndpoint("/websocket")
|
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
|
||||||
.setAllowedOrigins("*")
|
|
||||||
.withSockJS();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureInbound(MessageSecurityMetadataSourceRegistry message) {
|
protected void configureInbound(MessageSecurityMetadataSourceRegistry message) {
|
||||||
message
|
message.nullDestMatcher().permitAll().simpDestMatchers("/app/**").permitAll()
|
||||||
.nullDestMatcher().permitAll()
|
.simpSubscribeDestMatchers("/topic/**").permitAll().anyMessage().denyAll();
|
||||||
.simpDestMatchers("/app/**").permitAll()
|
|
||||||
.simpSubscribeDestMatchers("/topic/**").permitAll()
|
|
||||||
.anyMessage().denyAll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean sameOriginDisabled() {
|
protected boolean sameOriginDisabled() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,7 @@ public class BaseDTO implements Serializable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "BaseDTO{" +
|
return "BaseDTO{" + "id=" + id + '}';
|
||||||
"id=" + id +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,5 +50,4 @@ public class NamedDTO extends BaseDTO {
|
||||||
return getName().equals(namedDTO.getName());
|
return getName().equals(namedDTO.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,4 +15,5 @@ public class RoleDTO extends NamedDTO implements Serializable {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString();
|
return super.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,41 @@ package org.springframework.samples.petclinic.model.common;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
@Entity(name = "Role")
|
@Entity(name = "Role")
|
||||||
@Table(name = "roles")
|
@Table(name = "roles")
|
||||||
public class Role extends NamedEntity implements Serializable {
|
public class Role implements Serializable {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
@ManyToMany(mappedBy = "roles")
|
||||||
|
private Collection<User> users;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<User> getUsers() {
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsers(Collection<User> users) {
|
||||||
|
this.users = users;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,9 +54,9 @@ public class User extends Person implements Serializable, UserDetails {
|
||||||
private boolean credentialsNonExpired;
|
private boolean credentialsNonExpired;
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"),
|
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "role_id"))
|
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
|
||||||
private Set<Role> roles;
|
private Collection<Role> roles;
|
||||||
|
|
||||||
@Size(max = CommonParameter.PHONE_MAX, message = CommonError.FORMAT_LESS + CommonParameter.PHONE_MAX)
|
@Size(max = CommonParameter.PHONE_MAX, message = CommonError.FORMAT_LESS + CommonParameter.PHONE_MAX)
|
||||||
// @Pattern(regexp = CommonParameter.PHONE_REGEXP, message = CommonError.PHONE_FORMAT)
|
// @Pattern(regexp = CommonParameter.PHONE_REGEXP, message = CommonError.PHONE_FORMAT)
|
||||||
|
@ -145,6 +145,14 @@ public class User extends Person implements Serializable, UserDetails {
|
||||||
this.credentialsNonExpired = credentialsNonExpired;
|
this.credentialsNonExpired = credentialsNonExpired;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<Role> getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoles(Collection<Role> roles) {
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
||||||
|
@ -154,35 +162,6 @@ public class User extends Person implements Serializable, UserDetails {
|
||||||
return grantedAuthorities;
|
return grantedAuthorities;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Set<Role> getRolesInternal() {
|
|
||||||
if (this.roles == null) {
|
|
||||||
this.roles = new HashSet<>();
|
|
||||||
}
|
|
||||||
return this.roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setRolesInternal(Set<Role> roles) {
|
|
||||||
this.roles = roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public List<Role> getRoles() {
|
|
||||||
List<Role> sortedRoles = new ArrayList<>(getRolesInternal());
|
|
||||||
PropertyComparator.sort(sortedRoles, new MutableSortDefinition("name", true, true));
|
|
||||||
return Collections.unmodifiableList(sortedRoles);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getNrOfRoles() {
|
|
||||||
return getRolesInternal().size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addRole(Role role) {
|
|
||||||
getRolesInternal().add(role);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRoles(Set<Role> roles) {
|
|
||||||
this.roles = roles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTelephone() {
|
public String getTelephone() {
|
||||||
return telephone;
|
return telephone;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import org.springframework.samples.petclinic.model.common.Role;
|
import org.springframework.samples.petclinic.model.common.Role;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repository class for <code>Role</code> domain objects All method names are compliant
|
* Repository class for <code>Role</code> domain objects All method names are compliant
|
||||||
|
@ -31,7 +31,7 @@ public interface RoleRepository extends Repository<Role, Integer> {
|
||||||
* Retrieve all {@link Role}s from the data store
|
* Retrieve all {@link Role}s from the data store
|
||||||
* @return a Collection of {@link Role}s (or an empty Collection if none
|
* @return a Collection of {@link Role}s (or an empty Collection if none
|
||||||
*/
|
*/
|
||||||
List<Role> findAll();
|
Collection<Role> findAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a {@link Role} to the data store, either inserting or updating it.
|
* Save a {@link Role} to the data store, either inserting or updating it.
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.repository;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.samples.petclinic.model.common.User;
|
import org.springframework.samples.petclinic.model.common.User;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +24,7 @@ public interface UserRepository extends Repository<User, Integer> {
|
||||||
* Retrieve all {@link User}s from the data store
|
* Retrieve all {@link User}s from the data store
|
||||||
* @return a Collection of {@link User}s (or an empty Collection if none
|
* @return a Collection of {@link User}s (or an empty Collection if none
|
||||||
*/
|
*/
|
||||||
List<User> findAll();
|
Collection<User> findAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save an {@link User} to the data store, either inserting or updating it.
|
* Save an {@link User} to the data store, either inserting or updating it.
|
||||||
|
@ -38,4 +39,5 @@ public interface UserRepository extends Repository<User, Integer> {
|
||||||
* @return the deleted {@link User}
|
* @return the deleted {@link User}
|
||||||
*/
|
*/
|
||||||
User delete(User user);
|
User delete(User user);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.springframework.samples.petclinic.service.business.BaseService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,7 +70,14 @@ public class RoleService implements BaseService<Role, RoleDTO> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<RoleDTO> findAll() {
|
public List<RoleDTO> findAll() {
|
||||||
return entitiesToDTOS(roleRepository.findAll());
|
Collection<Role> roles = roleRepository.findAll();
|
||||||
|
List<RoleDTO> roleDTOS = new ArrayList<>();
|
||||||
|
|
||||||
|
roles.forEach(role -> {
|
||||||
|
roleDTOS.add(entityToDTO(role));
|
||||||
|
});
|
||||||
|
|
||||||
|
return roleDTOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -5,11 +5,13 @@ import org.springframework.samples.petclinic.dto.common.RoleDTO;
|
||||||
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
import org.springframework.samples.petclinic.model.common.Role;
|
import org.springframework.samples.petclinic.model.common.Role;
|
||||||
import org.springframework.samples.petclinic.model.common.User;
|
import org.springframework.samples.petclinic.model.common.User;
|
||||||
|
import org.springframework.samples.petclinic.repository.RoleRepository;
|
||||||
import org.springframework.samples.petclinic.repository.UserRepository;
|
import org.springframework.samples.petclinic.repository.UserRepository;
|
||||||
import org.springframework.samples.petclinic.service.business.BaseService;
|
import org.springframework.samples.petclinic.service.business.BaseService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,9 +90,14 @@ public class UserService implements BaseService<User, UserDTO> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<UserDTO> findAll() {
|
public List<UserDTO> findAll() {
|
||||||
List<User> users = userRepository.findAll();
|
Collection<User> users = userRepository.findAll();
|
||||||
|
List<UserDTO> userDTOS = new ArrayList<>();
|
||||||
|
|
||||||
return entitiesToDTOS(users);
|
users.forEach(user -> {
|
||||||
|
userDTOS.add(entityToDTO(user));
|
||||||
|
});
|
||||||
|
|
||||||
|
return userDTOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -105,6 +112,7 @@ public class UserService implements BaseService<User, UserDTO> {
|
||||||
public UserDTO findByEmail(String email) {
|
public UserDTO findByEmail(String email) {
|
||||||
User user = userRepository.findByEmail(email);
|
User user = userRepository.findByEmail(email);
|
||||||
|
|
||||||
|
|
||||||
return entityToDTO(user);
|
return entityToDTO(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,21 +20,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
@Override
|
@Override
|
||||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||||
auth.inMemoryAuthentication()
|
auth.inMemoryAuthentication().passwordEncoder(encoder).withUser(TEST_USER).password(encoder.encode("secret"))
|
||||||
.passwordEncoder(encoder)
|
|
||||||
.withUser(TEST_USER)
|
|
||||||
.password(encoder.encode("secret"))
|
|
||||||
.roles("ROLE_USER");
|
.roles("ROLE_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests()
|
http.authorizeRequests().antMatchers("/owners/**", "/pets/**", "/users/**", "/visits/**").authenticated()
|
||||||
.antMatchers("/owners/**", "/pets/**", "/users/**", "/visits/**")
|
.antMatchers("/**").permitAll().and().httpBasic();
|
||||||
.authenticated()
|
|
||||||
.antMatchers("/**")
|
|
||||||
.permitAll()
|
|
||||||
.and()
|
|
||||||
.httpBasic();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue