mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
modify role
This commit is contained in:
parent
d6ddf2c216
commit
bb5391bca9
12 changed files with 92 additions and 78 deletions
|
@ -17,19 +17,16 @@ import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
|||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
||||
|
||||
/*
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
|
||||
stompEndpointRegistry.addEndpoint("/websocket").withSockJS();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
registry.enableSimpleBroker("/topic");
|
||||
registry.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
* @Override public void registerStompEndpoints(StompEndpointRegistry
|
||||
* stompEndpointRegistry) {
|
||||
* stompEndpointRegistry.addEndpoint("/websocket").withSockJS(); }
|
||||
*
|
||||
* @Override public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
* registry.enableSimpleBroker("/topic");
|
||||
* registry.setApplicationDestinationPrefixes("/app"); }
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic");
|
||||
|
@ -38,22 +35,18 @@ public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfi
|
|||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/websocket")
|
||||
.setAllowedOrigins("*")
|
||||
.withSockJS();
|
||||
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureInbound(MessageSecurityMetadataSourceRegistry message) {
|
||||
message
|
||||
.nullDestMatcher().permitAll()
|
||||
.simpDestMatchers("/app/**").permitAll()
|
||||
.simpSubscribeDestMatchers("/topic/**").permitAll()
|
||||
.anyMessage().denyAll();
|
||||
message.nullDestMatcher().permitAll().simpDestMatchers("/app/**").permitAll()
|
||||
.simpSubscribeDestMatchers("/topic/**").permitAll().anyMessage().denyAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean sameOriginDisabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public class UserController extends WebSocketSender {
|
|||
|
||||
@InitBinder("user")
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
// dataBinder.setDisallowedFields(CommonAttribute.USER_ID,"roles");
|
||||
// dataBinder.setDisallowedFields(CommonAttribute.USER_ID,"roles");
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -41,8 +41,7 @@ public class BaseDTO implements Serializable {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BaseDTO{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
return "BaseDTO{" + "id=" + id + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -50,5 +50,4 @@ public class NamedDTO extends BaseDTO {
|
|||
return getName().equals(namedDTO.getName());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,5 @@ public class RoleDTO extends NamedDTO implements Serializable {
|
|||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,41 @@ package org.springframework.samples.petclinic.model.common;
|
|||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
|
||||
@Entity(name = "Role")
|
||||
@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;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "role_id"))
|
||||
private Set<Role> roles;
|
||||
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
|
||||
private Collection<Role> roles;
|
||||
|
||||
@Size(max = CommonParameter.PHONE_MAX, message = CommonError.FORMAT_LESS + CommonParameter.PHONE_MAX)
|
||||
// @Pattern(regexp = CommonParameter.PHONE_REGEXP, message = CommonError.PHONE_FORMAT)
|
||||
|
@ -145,6 +145,14 @@ public class User extends Person implements Serializable, UserDetails {
|
|||
this.credentialsNonExpired = credentialsNonExpired;
|
||||
}
|
||||
|
||||
public Collection<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Collection<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
||||
|
@ -154,35 +162,6 @@ public class User extends Person implements Serializable, UserDetails {
|
|||
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() {
|
||||
return telephone;
|
||||
|
|
|
@ -3,7 +3,7 @@ package org.springframework.samples.petclinic.repository;
|
|||
import org.springframework.samples.petclinic.model.common.Role;
|
||||
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
|
||||
|
@ -31,7 +31,7 @@ public interface RoleRepository extends Repository<Role, Integer> {
|
|||
* Retrieve all {@link Role}s from the data store
|
||||
* @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.
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.springframework.samples.petclinic.repository;
|
|||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.samples.petclinic.model.common.User;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -23,7 +24,7 @@ public interface UserRepository extends Repository<User, Integer> {
|
|||
* Retrieve all {@link User}s from the data store
|
||||
* @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.
|
||||
|
@ -33,9 +34,10 @@ public interface UserRepository extends Repository<User, Integer> {
|
|||
User save(User user);
|
||||
|
||||
/**
|
||||
* Delete an {@link User} to the data store.
|
||||
* Delete an {@link User} to the data store.
|
||||
* @param user the {@link User} to delete
|
||||
* @return the deleted {@link User}
|
||||
*/
|
||||
User delete(User user);
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.springframework.samples.petclinic.service.business.BaseService;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -69,7 +70,14 @@ public class RoleService implements BaseService<Role, RoleDTO> {
|
|||
|
||||
@Override
|
||||
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
|
||||
|
|
|
@ -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.model.common.Role;
|
||||
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.service.business.BaseService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -88,9 +90,14 @@ public class UserService implements BaseService<User, UserDTO> {
|
|||
|
||||
@Override
|
||||
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
|
||||
|
@ -105,6 +112,7 @@ public class UserService implements BaseService<User, UserDTO> {
|
|||
public UserDTO findByEmail(String email) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
|
||||
|
||||
return entityToDTO(user);
|
||||
}
|
||||
|
||||
|
|
|
@ -20,21 +20,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
auth.inMemoryAuthentication()
|
||||
.passwordEncoder(encoder)
|
||||
.withUser(TEST_USER)
|
||||
.password(encoder.encode("secret"))
|
||||
.roles("ROLE_USER");
|
||||
auth.inMemoryAuthentication().passwordEncoder(encoder).withUser(TEST_USER).password(encoder.encode("secret"))
|
||||
.roles("ROLE_USER");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/owners/**", "/pets/**", "/users/**", "/visits/**")
|
||||
.authenticated()
|
||||
.antMatchers("/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
http.authorizeRequests().antMatchers("/owners/**", "/pets/**", "/users/**", "/visits/**").authenticated()
|
||||
.antMatchers("/**").permitAll().and().httpBasic();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue