燕鹏
4 years ago
15 changed files with 416 additions and 10 deletions
@ -0,0 +1,36 @@ |
|||||
|
package com.aiprose.scauth.controller; |
||||
|
|
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import com.aiprose.scauth.entity.User; |
||||
|
import com.aiprose.scauth.entity.UserRole; |
||||
|
import com.aiprose.scauth.service.IRoleService; |
||||
|
import com.aiprose.scauth.service.IUserService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:21 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@RestController() |
||||
|
@RequestMapping("role") |
||||
|
public class RoleController { |
||||
|
|
||||
|
@Autowired |
||||
|
private IRoleService roleService; |
||||
|
|
||||
|
// roleName 必须要有ROLE_前缀
|
||||
|
@PostMapping("save") |
||||
|
public UserRole save(String roleName,Integer userId){ |
||||
|
Role role = new Role(); |
||||
|
role.setRole(roleName); |
||||
|
UserRole userRole = roleService.save(role,userId); |
||||
|
return userRole; |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package com.aiprose.scauth.controller; |
||||
|
|
||||
|
import com.aiprose.scauth.entity.User; |
||||
|
import com.aiprose.scauth.service.IUserService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:21 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@RestController() |
||||
|
@RequestMapping("user") |
||||
|
public class UserController { |
||||
|
|
||||
|
@Autowired |
||||
|
private IUserService userService; |
||||
|
|
||||
|
@PostMapping("save") |
||||
|
public User save(User user){ |
||||
|
String encode = new BCryptPasswordEncoder().encode(user.getPassword()); |
||||
|
user.setPassword(encode); |
||||
|
User save = userService.save(user); |
||||
|
return save; |
||||
|
} |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
package com.aiprose.scauth.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
@Data |
||||
|
@MappedSuperclass |
||||
|
public class IDEntity implements Serializable { |
||||
|
/** |
||||
|
* 主键 |
||||
|
*/ |
||||
|
@Id |
||||
|
@Column(columnDefinition = "INT UNSIGNED") |
||||
|
@GeneratedValue(strategy = GenerationType.AUTO) |
||||
|
protected Integer id; |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.aiprose.scauth.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.springframework.security.core.GrantedAuthority; |
||||
|
|
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Table; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc 角色表 |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:04 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Entity |
||||
|
@Table(name="sys_role") |
||||
|
public class Role extends IDEntity implements GrantedAuthority { |
||||
|
private String role; |
||||
|
|
||||
|
@Override |
||||
|
public String getAuthority() { |
||||
|
return role; |
||||
|
} |
||||
|
} |
@ -0,0 +1,56 @@ |
|||||
|
package com.aiprose.scauth.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.springframework.security.core.GrantedAuthority; |
||||
|
import org.springframework.security.core.userdetails.UserDetails; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Table; |
||||
|
import javax.persistence.Transient; |
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc 用户表 |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:04 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Entity |
||||
|
@Table(name="sys_user") |
||||
|
public class User extends IDEntity implements UserDetails { |
||||
|
@Column(unique = true) |
||||
|
private String username; |
||||
|
private String password; |
||||
|
|
||||
|
@Transient |
||||
|
private List<Role> roles; |
||||
|
|
||||
|
@Override |
||||
|
public Collection<? extends GrantedAuthority> getAuthorities() { |
||||
|
return roles; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean isAccountNonExpired() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean isAccountNonLocked() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean isCredentialsNonExpired() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean isEnabled() { |
||||
|
return true; |
||||
|
} |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package com.aiprose.scauth.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.Table; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc 用户角色关联表 |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:04 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Entity |
||||
|
@Table(name="sys_user_role") |
||||
|
public class UserRole extends IDEntity{ |
||||
|
private Integer sysUserId; |
||||
|
private Integer sysRoleId; |
||||
|
|
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.aiprose.scauth.repository; |
||||
|
|
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:17 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
public interface RoleRepository extends JpaRepository<Role, Integer>, JpaSpecificationExecutor<Role> { |
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
package com.aiprose.scauth.repository; |
||||
|
|
||||
|
import com.aiprose.scauth.entity.User; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:17 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> { |
||||
|
|
||||
|
User findByUsername(String username); |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package com.aiprose.scauth.repository; |
||||
|
|
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import com.aiprose.scauth.entity.UserRole; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:17 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
public interface UserRoleRepository extends JpaRepository<UserRole, Integer>, JpaSpecificationExecutor<UserRole> { |
||||
|
List<UserRole> findBySysUserId(Integer userId); |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package com.aiprose.scauth.service; |
||||
|
|
||||
|
import com.aiprose.base.BaseSpecService; |
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import com.aiprose.scauth.entity.UserRole; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:23 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
public interface IRoleService extends BaseSpecService<Role,Integer> { |
||||
|
List<Role> findByUserId(Integer userId); |
||||
|
|
||||
|
UserRole save(Role role, Integer userId); |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package com.aiprose.scauth.service; |
||||
|
|
||||
|
import com.aiprose.base.BaseSpecService; |
||||
|
import com.aiprose.scauth.entity.User; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:23 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
public interface IUserService extends BaseSpecService<User,Integer> { |
||||
|
User findByUsername(String username); |
||||
|
User findByUsernameAndRole(String username); |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
package com.aiprose.scauth.service.impl; |
||||
|
|
||||
|
import com.aiprose.base.BaseSpecServiceImpl; |
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import com.aiprose.scauth.entity.UserRole; |
||||
|
import com.aiprose.scauth.repository.RoleRepository; |
||||
|
import com.aiprose.scauth.repository.UserRoleRepository; |
||||
|
import com.aiprose.scauth.service.IRoleService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.transaction.Transactional; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:22 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@Service |
||||
|
@Transactional |
||||
|
public class RoleServiceImpl extends BaseSpecServiceImpl<RoleRepository, Role, Integer> implements IRoleService { |
||||
|
|
||||
|
@Autowired |
||||
|
private UserRoleRepository userRoleRepository; |
||||
|
|
||||
|
@Override |
||||
|
public List<Role> findByUserId(Integer userId) { |
||||
|
List<UserRole> userRoles = userRoleRepository.findBySysUserId(userId); |
||||
|
if (userRoles == null) { |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
List<Integer> roleIds = userRoles.stream().map(x -> x.getSysRoleId()).collect(Collectors.toList()); |
||||
|
List<Role> allById = repository.findAllById(roleIds); |
||||
|
if (allById != null) { |
||||
|
return allById; |
||||
|
} |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserRole save(Role role, Integer userId) { |
||||
|
Role newRole = repository.save(role); |
||||
|
UserRole userRole = new UserRole(); |
||||
|
userRole.setSysUserId(userId); |
||||
|
userRole.setSysRoleId(newRole.getId()); |
||||
|
UserRole save = userRoleRepository.save(userRole); |
||||
|
return save; |
||||
|
} |
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
package com.aiprose.scauth.service.impl; |
||||
|
|
||||
|
import com.aiprose.base.BaseSpecServiceImpl; |
||||
|
import com.aiprose.scauth.entity.Role; |
||||
|
import com.aiprose.scauth.entity.User; |
||||
|
import com.aiprose.scauth.repository.UserRepository; |
||||
|
import com.aiprose.scauth.service.IRoleService; |
||||
|
import com.aiprose.scauth.service.IUserService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import javax.transaction.Transactional; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author nelson |
||||
|
* @desc TODO |
||||
|
* @company 北京中经网软件有限公司 |
||||
|
* @date 2020/11/27 17:22 |
||||
|
* @since 1.0 |
||||
|
*/ |
||||
|
@Service |
||||
|
@Transactional |
||||
|
public class UserServiceImpl extends BaseSpecServiceImpl<UserRepository, User, Integer> implements IUserService { |
||||
|
|
||||
|
@Autowired |
||||
|
private IRoleService roleService; |
||||
|
|
||||
|
@Override |
||||
|
public User findByUsername(String username) { |
||||
|
return repository.findByUsername(username); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public User findByUsernameAndRole(String username) { |
||||
|
User byUsername = repository.findByUsername(username); |
||||
|
if(byUsername!=null){ |
||||
|
List<Role> byUserId = roleService.findByUserId(byUsername.getId()); |
||||
|
byUsername.setRoles(byUserId); |
||||
|
} |
||||
|
return byUsername; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue