作者 朱兆平

dev 权限部分

@@ -3,7 +3,9 @@ @@ -3,7 +3,9 @@
3 /logs/ 3 /logs/
4 /out/ 4 /out/
5 .mvn 5 .mvn
6 - 6 +/lib/
  7 +/xmlFromImf/
  8 +/errorLogs/
7 ### STS ### 9 ### STS ###
8 .apt_generated 10 .apt_generated
9 .classpath 11 .classpath
@@ -74,6 +74,13 @@ @@ -74,6 +74,13 @@
74 </exclusions> 74 </exclusions>
75 </dependency> 75 </dependency>
76 <!--spring boot--> 76 <!--spring boot-->
  77 + <!-- tools-->
  78 + <dependency>
  79 + <groupId>commons-codec</groupId>
  80 + <artifactId>commons-codec</artifactId>
  81 + <version>1.11</version>
  82 + </dependency>
  83 + <!-- tools-->
77 <!-- alibaba的druid数据库连接池 --> 84 <!-- alibaba的druid数据库连接池 -->
78 <dependency> 85 <dependency>
79 <groupId>com.alibaba</groupId> 86 <groupId>com.alibaba</groupId>
  1 +package com.tianbo.warehouse.dao;
  2 +
  3 +import com.tianbo.warehouse.model.PERMISSION;
  4 +import java.math.BigDecimal;
  5 +import java.util.List;
  6 +
  7 +public interface PERMISSIONMapper {
  8 + int deleteByPrimaryKey(BigDecimal permissionId);
  9 +
  10 + int insert(PERMISSION record);
  11 +
  12 + int insertSelective(PERMISSION record);
  13 +
  14 + List<PERMISSION> selectByPrimaryKey(BigDecimal permissionId);
  15 +
  16 + int updateByPrimaryKeySelective(PERMISSION record);
  17 +
  18 + int updateByPrimaryKey(PERMISSION record);
  19 +
  20 + List<PERMISSION> findAll();
  21 +
  22 + List<PERMISSION> findByUserId(BigDecimal userId);
  23 +}
  1 +package com.tianbo.warehouse.dao;
  2 +
  3 +import com.tianbo.warehouse.model.ROLE;
  4 +import java.math.BigDecimal;
  5 +import java.util.List;
  6 +
  7 +public interface ROLEMapper {
  8 + int deleteByPrimaryKey(BigDecimal roleId);
  9 +
  10 + int insert(ROLE record);
  11 +
  12 + int insertSelective(ROLE record);
  13 +
  14 + ROLE selectByPrimaryKey(BigDecimal roleId);
  15 +
  16 + int updateByPrimaryKeySelective(ROLE record);
  17 +
  18 + int updateByPrimaryKey(ROLE record);
  19 +
  20 + List<ROLE> findRolesByUserId(BigDecimal userId);
  21 +}
  1 +package com.tianbo.warehouse.dao;
  2 +
  3 +import com.tianbo.warehouse.model.ROLE;
  4 +import com.tianbo.warehouse.model.USERS;
  5 +import java.math.BigDecimal;
  6 +import java.util.List;
  7 +
  8 +public interface USERSMapper {
  9 + int deleteByPrimaryKey(BigDecimal userId);
  10 +
  11 + int insert(USERS record);
  12 +
  13 + int insertSelective(USERS record);
  14 +
  15 + USERS selectByPrimaryKey(BigDecimal userId);
  16 +
  17 + List<USERS> selectByUsername(String userName);
  18 +
  19 + int updateByPrimaryKeySelective(USERS record);
  20 +
  21 + int updateByPrimaryKey(USERS record);
  22 +
  23 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import java.math.BigDecimal;
  4 +
  5 +public class PERMISSION {
  6 + private BigDecimal permissionId;
  7 +
  8 + private String permissionName;
  9 +
  10 + private String permissionSign;
  11 +
  12 + private String description;
  13 +
  14 + private String groupName;
  15 +
  16 + public BigDecimal getPermissionId() {
  17 + return permissionId;
  18 + }
  19 +
  20 + public void setPermissionId(BigDecimal permissionId) {
  21 + this.permissionId = permissionId;
  22 + }
  23 +
  24 + public String getPermissionName() {
  25 + return permissionName;
  26 + }
  27 +
  28 + public void setPermissionName(String permissionName) {
  29 + this.permissionName = permissionName == null ? null : permissionName.trim();
  30 + }
  31 +
  32 + public String getPermissionSign() {
  33 + return permissionSign;
  34 + }
  35 +
  36 + public void setPermissionSign(String permissionSign) {
  37 + this.permissionSign = permissionSign == null ? null : permissionSign.trim();
  38 + }
  39 +
  40 + public String getDescription() {
  41 + return description;
  42 + }
  43 +
  44 + public void setDescription(String description) {
  45 + this.description = description == null ? null : description.trim();
  46 + }
  47 +
  48 + public String getGroupName() {
  49 + return groupName;
  50 + }
  51 +
  52 + public void setGroupName(String groupName) {
  53 + this.groupName = groupName == null ? null : groupName.trim();
  54 + }
  55 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import org.springframework.security.core.GrantedAuthority;
  4 +
  5 +import java.math.BigDecimal;
  6 +
  7 +public class ROLE implements GrantedAuthority{
  8 +
  9 + private static final long serialVersionUID = -3957539165716897200L;
  10 +
  11 + private BigDecimal roleId;
  12 +
  13 + private String roleName;
  14 +
  15 + private String roleSign;
  16 +
  17 + private String description;
  18 +
  19 + public BigDecimal getRoleId() {
  20 + return roleId;
  21 + }
  22 +
  23 + public void setRoleId(BigDecimal roleId) {
  24 + this.roleId = roleId;
  25 + }
  26 +
  27 + public String getRoleName() {
  28 + return roleName;
  29 + }
  30 +
  31 + public void setRoleName(String roleName) {
  32 + this.roleName = roleName == null ? null : roleName.trim();
  33 + }
  34 +
  35 + public String getRoleSign() {
  36 + return roleSign;
  37 + }
  38 +
  39 + public void setRoleSign(String roleSign) {
  40 + this.roleSign = roleSign == null ? null : roleSign.trim();
  41 + }
  42 +
  43 + public String getDescription() {
  44 + return description;
  45 + }
  46 +
  47 + public void setDescription(String description) {
  48 + this.description = description == null ? null : description.trim();
  49 + }
  50 +
  51 + @Override
  52 + public String getAuthority(){
  53 + return this.getRoleName();
  54 + }
  55 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import org.springframework.security.core.GrantedAuthority;
  4 +import org.springframework.security.core.authority.SimpleGrantedAuthority;
  5 +import org.springframework.security.core.userdetails.UserDetails;
  6 +
  7 +import java.io.Serializable;
  8 +import java.math.BigDecimal;
  9 +import java.util.ArrayList;
  10 +import java.util.Collection;
  11 +import java.util.Date;
  12 +import java.util.List;
  13 +
  14 +public class USERS implements UserDetails{
  15 +
  16 + private static final long serialVersionUID = 1L;
  17 +
  18 + private BigDecimal userId;
  19 +
  20 + private String username;
  21 +
  22 + private String password;
  23 +
  24 + private Short state;
  25 +
  26 + private Date creattime;
  27 +
  28 + private String sex;
  29 +
  30 + private String address;
  31 +
  32 + private Date birthday;
  33 +
  34 + private String mobilephone;
  35 +
  36 + private Date updatetime;
  37 +
  38 + private List<ROLE> roles;
  39 +
  40 + public BigDecimal getUserId() {
  41 + return userId;
  42 + }
  43 +
  44 + public void setUserId(BigDecimal userId) {
  45 + this.userId = userId;
  46 + }
  47 +
  48 + @Override
  49 + public String getUsername() {
  50 + return username;
  51 + }
  52 +
  53 + public void setUsername(String username) {
  54 + this.username = username == null ? null : username.trim();
  55 + }
  56 +
  57 + @Override
  58 + public String getPassword() {
  59 + return password;
  60 + }
  61 +
  62 + public void setPassword(String password) {
  63 + this.password = password == null ? null : password.trim();
  64 + }
  65 +
  66 + public Short getState() {
  67 + return state;
  68 + }
  69 +
  70 + public void setState(Short state) {
  71 + this.state = state;
  72 + }
  73 +
  74 + public Date getCreattime() {
  75 + return creattime;
  76 + }
  77 +
  78 + public void setCreattime(Date creattime) {
  79 + this.creattime = creattime;
  80 + }
  81 +
  82 + public String getSex() {
  83 + return sex;
  84 + }
  85 +
  86 + public void setSex(String sex) {
  87 + this.sex = sex == null ? null : sex.trim();
  88 + }
  89 +
  90 + public String getAddress() {
  91 + return address;
  92 + }
  93 +
  94 + public void setAddress(String address) {
  95 + this.address = address == null ? null : address.trim();
  96 + }
  97 +
  98 + public Date getBirthday() {
  99 + return birthday;
  100 + }
  101 +
  102 + public void setBirthday(Date birthday) {
  103 + this.birthday = birthday;
  104 + }
  105 +
  106 + public String getMobilephone() {
  107 + return mobilephone;
  108 + }
  109 +
  110 + public void setMobilephone(String mobilephone) {
  111 + this.mobilephone = mobilephone == null ? null : mobilephone.trim();
  112 + }
  113 +
  114 + public Date getUpdatetime() {
  115 + return updatetime;
  116 + }
  117 +
  118 + public void setUpdatetime(Date updatetime) {
  119 + this.updatetime = updatetime;
  120 + }
  121 +
  122 + public List<ROLE> getRoles() {
  123 + return roles;
  124 + }
  125 +
  126 + public void setRoles(List<ROLE> roles) {
  127 + this.roles = roles;
  128 + }
  129 +
  130 + @Override
  131 + public boolean isAccountNonExpired(){
  132 + return true;
  133 + }
  134 +
  135 + @Override
  136 + public boolean isAccountNonLocked(){
  137 + return true;
  138 + }
  139 +
  140 + @Override
  141 + public boolean isCredentialsNonExpired(){
  142 + return true;
  143 + }
  144 +
  145 + @Override
  146 + public boolean isEnabled(){
  147 + return true;
  148 + }
  149 +
  150 + @Override
  151 + public Collection<? extends GrantedAuthority> getAuthorities(){
  152 + List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
  153 + List<ROLE> roles = this.getRoles();
  154 + for (ROLE role : roles) {
  155 + auths.add(new SimpleGrantedAuthority(role.getAuthority()));
  156 + }
  157 + return auths;
  158 + }
  159 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import com.tianbo.warehouse.dao.PERMISSIONMapper;
  4 +import com.tianbo.warehouse.dao.USERSMapper;
  5 +import com.tianbo.warehouse.model.PERMISSION;
  6 +import com.tianbo.warehouse.model.USERS;
  7 +import com.tianbo.warehouse.service.UserService;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.security.core.GrantedAuthority;
  10 +import org.springframework.security.core.authority.SimpleGrantedAuthority;
  11 +import org.springframework.security.core.userdetails.User;
  12 +import org.springframework.security.core.userdetails.UserDetails;
  13 +import org.springframework.security.core.userdetails.UserDetailsService;
  14 +import org.springframework.security.core.userdetails.UsernameNotFoundException;
  15 +import org.springframework.stereotype.Service;
  16 +
  17 +import java.util.ArrayList;
  18 +import java.util.List;
  19 +
  20 +@Service("customuserservice")
  21 +public class CustomUserDetailService implements UserDetailsService {
  22 +
  23 + @Autowired
  24 + UserService userService;
  25 + @Autowired
  26 + PERMISSIONMapper permissionMapper;
  27 +
  28 + @Override
  29 + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  30 +
  31 + try {
  32 + USERS user = userService.loadByUsername(username);
  33 +
  34 + //这里是根据用户ID取的权限表,还可以根据用户ID 去角色表
  35 + List<PERMISSION> permissions = permissionMapper.findByUserId(user.getUserId());
  36 + List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
  37 + for (PERMISSION permission : permissions) {
  38 + if (permission != null && permission.getPermissionName() != null) {
  39 +
  40 + GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getPermissionName());
  41 + //1:此处将权限信息添加到 GrantedAuthority 对象中,在后面进行全权限验证时会使用GrantedAuthority 对象。
  42 + grantedAuthorities.add(grantedAuthority);
  43 + }
  44 + }
  45 + return user;
  46 + } catch (UsernameNotFoundException e) {
  47 +
  48 + e.printStackTrace();
  49 + }
  50 + return null;
  51 + }
  52 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import org.springframework.security.access.AccessDecisionManager;
  4 +import org.springframework.security.access.AccessDeniedException;
  5 +import org.springframework.security.access.ConfigAttribute;
  6 +import org.springframework.security.authentication.InsufficientAuthenticationException;
  7 +import org.springframework.security.core.Authentication;
  8 +import org.springframework.security.core.GrantedAuthority;
  9 +import org.springframework.stereotype.Service;
  10 +
  11 +import java.util.Collection;
  12 +import java.util.Iterator;
  13 +
  14 +@Service
  15 +public class MyAccessDecisionManager implements AccessDecisionManager{
  16 +
  17 + // decide 方法是判定是否拥有权限的决策方法,
  18 + //authentication 是释CustomUserService中循环添加到 GrantedAuthority 对象中的权限信息集合.
  19 + //object 包含客户端发起的请求的requset信息,可转换为 HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
  20 + //configAttributes 为MyInvocationSecurityMetadataSource的getAttributes(Object object)这个方法返回的结果,此方法是为了判定用户请求的url 是否在权限表中,如果在权限表中,则返回给 decide 方法,用来判定用户是否有此权限。如果不在权限表中则放行。
  21 +
  22 + @Override
  23 + public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException{
  24 +
  25 + if(null== configAttributes || configAttributes.size() <=0) {
  26 + return;
  27 + }
  28 + ConfigAttribute c;
  29 + String needRole;
  30 + for(Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext(); ) {
  31 + c = iter.next();
  32 + needRole = c.getAttribute();
  33 + //authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合
  34 + for(GrantedAuthority ga : authentication.getAuthorities()) {
  35 + if(needRole.trim().equals(ga.getAuthority())) {
  36 + return;
  37 + }
  38 + }
  39 + }
  40 + throw new AccessDeniedException("no right");
  41 + }
  42 +
  43 + @Override
  44 + public boolean supports(ConfigAttribute var1){
  45 +
  46 + return true;
  47 + }
  48 +
  49 + @Override
  50 + public boolean supports(Class<?> var1){
  51 + return true;
  52 + }
  53 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.security.access.SecurityMetadataSource;
  5 +import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
  6 +import org.springframework.security.access.intercept.InterceptorStatusToken;
  7 +import org.springframework.security.web.FilterInvocation;
  8 +import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
  9 +import org.springframework.stereotype.Service;
  10 +
  11 +import javax.servlet.*;
  12 +import java.io.IOException;
  13 +
  14 +@Service
  15 +public class MyFilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {
  16 +
  17 + @Autowired
  18 + private FilterInvocationSecurityMetadataSource securityMetadataSource;
  19 +
  20 +
  21 + @Autowired
  22 + public void setMyAccessDecisionManager(MyAccessDecisionManager myAccessDecisionManager) {
  23 + super.setAccessDecisionManager(myAccessDecisionManager);
  24 + }
  25 +
  26 +
  27 + @Override
  28 + public void init(FilterConfig filterConfig) throws ServletException {
  29 +
  30 + }
  31 +
  32 + @Override
  33 + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  34 +
  35 + FilterInvocation fi = new FilterInvocation(request, response, chain);
  36 + invoke(fi);
  37 + }
  38 +
  39 +
  40 + public void invoke(FilterInvocation fi) throws IOException, ServletException {
  41 +//fi里面有一个被拦截的url
  42 +//里面调用MyInvocationSecurityMetadataSource的getAttributes(Object object)这个方法获取fi对应的所有权限
  43 +//再调用MyAccessDecisionManager的decide方法来校验用户的权限是否足够
  44 + InterceptorStatusToken token = super.beforeInvocation(fi);
  45 + try {
  46 +//执行下一个拦截器
  47 + fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
  48 + } finally {
  49 + super.afterInvocation(token, null);
  50 + }
  51 + }
  52 +
  53 + @Override
  54 + public void destroy() {
  55 +
  56 + }
  57 +
  58 + @Override
  59 + public Class<?> getSecureObjectClass() {
  60 + return FilterInvocation.class;
  61 + }
  62 +
  63 + @Override
  64 + public SecurityMetadataSource obtainSecurityMetadataSource() {
  65 + return this.securityMetadataSource;
  66 + }
  67 +
  68 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import com.tianbo.warehouse.dao.PERMISSIONMapper;
  4 +import com.tianbo.warehouse.model.PERMISSION;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.security.access.ConfigAttribute;
  7 +import org.springframework.security.access.SecurityConfig;
  8 +import org.springframework.security.web.FilterInvocation;
  9 +import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
  10 +import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  11 +import org.springframework.stereotype.Service;
  12 +
  13 +import javax.servlet.http.HttpServletRequest;
  14 +import java.util.*;
  15 +
  16 +@Service
  17 +public class MyInvocationSecurityMetadataSourceService implements FilterInvocationSecurityMetadataSource{
  18 +
  19 + @Autowired
  20 + PERMISSIONMapper permissionMapper;
  21 +
  22 + private HashMap<String, Collection<ConfigAttribute>> map =null;
  23 +
  24 + /**
  25 + * 加载权限表中所有权限
  26 + */
  27 + public void loadResourceDefine(){
  28 + map = new HashMap<>();
  29 + Collection<ConfigAttribute> array;
  30 + ConfigAttribute cfg;
  31 + List<PERMISSION> permissions = permissionMapper.findAll();
  32 + for(PERMISSION permission : permissions) {
  33 + array = new ArrayList<>();
  34 + cfg = new SecurityConfig(permission.getPermissionName());
  35 + //此处只添加了用户的名字,其实还可以添加更多权限的信息,例如请求方法到ConfigAttribute的集合中去。此处添加的信息将会作为MyAccessDecisionManager类的decide的第三个参数。
  36 + array.add(cfg);
  37 + //用权限的getUrl() 作为map的key,用ConfigAttribute的集合作为 value,
  38 + map.put(permission.getPermissionSign(), array);
  39 + }
  40 +
  41 + }
  42 +
  43 + //此方法是为了判定用户请求的url 是否在权限表中,如果在权限表中,则返回给 decide 方法,用来判定用户是否有此权限。如果不在权限表中则放行。
  44 + @Override
  45 + public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
  46 + if(map ==null) {loadResourceDefine();}
  47 + //object 中包含用户请求的request 信息
  48 + HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
  49 + AntPathRequestMatcher matcher;
  50 + String resUrl;
  51 + for(Iterator<String> iter = map.keySet().iterator(); iter.hasNext(); ) {
  52 + resUrl = iter.next();
  53 + matcher = new AntPathRequestMatcher(resUrl);
  54 + if(matcher.matches(request)) {
  55 + return map.get(resUrl);
  56 + }
  57 + }
  58 + return null;
  59 + }
  60 +
  61 + @Override
  62 + public Collection<ConfigAttribute> getAllConfigAttributes() {
  63 + return null;
  64 + }
  65 +
  66 + @Override
  67 + public boolean supports(Class<?> clazz) {
  68 + return true;
  69 + }
  70 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import org.apache.commons.codec.digest.DigestUtils;
  4 +import org.apache.tomcat.util.security.MD5Encoder;
  5 +import org.springframework.security.crypto.password.PasswordEncoder;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +@Service(value = "passwordEncodeService")
  9 +public class PasswordEncoderImp implements PasswordEncoder{
  10 +
  11 + @Override
  12 + public String encode(CharSequence charSequence) {
  13 + String enncodePassword = DigestUtils.md5Hex(charSequence.toString());
  14 + return enncodePassword;
  15 + }
  16 +
  17 + @Override
  18 + public boolean matches(CharSequence rawPassword, String encodedPassword) {
  19 + //用户输入的登录密码
  20 + String endcodeInputPass = DigestUtils.md5Hex(rawPassword.toString());
  21 + return encodedPassword.equals(endcodeInputPass);
  22 + }
  23 +}
  1 +package com.tianbo.warehouse.security;
  2 +
  3 +import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.context.annotation.Bean;
  5 +import org.springframework.context.annotation.Configuration;
  6 +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7 +import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8 +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  9 +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  10 +import org.springframework.security.core.userdetails.UserDetailsService;
  11 +import org.springframework.security.crypto.password.PasswordEncoder;
  12 +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
  13 +
  14 +@Configuration
  15 +@EnableWebSecurity
  16 +public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  17 +
  18 + @Autowired
  19 + private MyFilterSecurityInterceptor myFilterSecurityInterceptor;
  20 +
  21 + @Autowired
  22 + private PasswordEncoder passwordEncoder;
  23 +
  24 + @Bean
  25 + UserDetailsService customUserService(){ //注册UserDetailsService 的bean
  26 + return new CustomUserDetailService();
  27 + }
  28 +
  29 + @Override
  30 + protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  31 + //user Details Service验证
  32 + auth.userDetailsService(customUserService()).passwordEncoder(passwordEncoder);
  33 + }
  34 +
  35 + @Override
  36 + protected void configure(HttpSecurity http) throws Exception {
  37 + http.authorizeRequests()
  38 + .anyRequest().authenticated() //任何请求,登录后可以访问
  39 + .and()
  40 + .formLogin()
  41 + .loginProcessingUrl("/home")
  42 + .loginPage("/login")
  43 + .failureUrl("/login?error")
  44 + .permitAll() //登录页面用户任意访问
  45 + .and()
  46 + .logout().permitAll(); //注销行为任意访问
  47 + http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
  48 + }
  49 +}
  1 +package com.tianbo.warehouse.service;
  2 +
  3 +import com.tianbo.warehouse.model.USERS;
  4 +
  5 +public interface UserService {
  6 + USERS loadByUsername(String username);
  7 +}
  1 +package com.tianbo.warehouse.service.imp;
  2 +
  3 +import com.tianbo.warehouse.dao.ROLEMapper;
  4 +import com.tianbo.warehouse.dao.USERSMapper;
  5 +import com.tianbo.warehouse.model.ROLE;
  6 +import com.tianbo.warehouse.model.USERS;
  7 +import com.tianbo.warehouse.service.UserService;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Service;
  10 +
  11 +import java.util.List;
  12 +
  13 +@Service(value = "userService")
  14 +public class UserServiceImpl implements UserService{
  15 +
  16 + @Autowired
  17 + private USERSMapper usersMapper;
  18 +
  19 + @Autowired
  20 + private ROLEMapper roleMapper;
  21 +
  22 + @Override
  23 + public USERS loadByUsername(String username){
  24 + List<USERS> userList = usersMapper.selectByUsername(username);
  25 +
  26 + if (userList != null && userList.size() > 0) {
  27 + USERS user = userList.get(0);
  28 +
  29 + List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
  30 + if (roleList!=null && roleList.size()>0){
  31 + user.setRoles(roleList);
  32 + return user;
  33 + }
  34 + }
  35 + return null;
  36 +
  37 + }
  38 +}
@@ -42,8 +42,8 @@ spring.datasource.druid.test-on-return=false @@ -42,8 +42,8 @@ spring.datasource.druid.test-on-return=false
42 spring.datasource.druid.default-auto-commit=true 42 spring.datasource.druid.default-auto-commit=true
43 43
44 mybatis.mapper-locations=classpath:mapping/*.xml 44 mybatis.mapper-locations=classpath:mapping/*.xml
45 -mybatis.type-aliases-package=com.example.demo.model  
46 -logging.level.com.example.demo.mapper=DEBUG 45 +mybatis.type-aliases-package=com.tianbo.warehouse.model
  46 +logging.level.com.tianbo.warehouse.dao=DEBUG
47 47
48 pagehelper.helper-dialect=oracle 48 pagehelper.helper-dialect=oracle
49 #pagehelper.auto-dialect=true 49 #pagehelper.auto-dialect=true
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.tianbo.warehouse.dao.PERMISSIONMapper" >
  4 + <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.PERMISSION" >
  5 + <id column="PERMISSION_ID" property="permissionId" jdbcType="DECIMAL" />
  6 + <result column="PERMISSION_NAME" property="permissionName" jdbcType="VARCHAR" />
  7 + <result column="PERMISSION_SIGN" property="permissionSign" jdbcType="VARCHAR" />
  8 + <result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
  9 + <result column="GROUP_NAME" property="groupName" jdbcType="VARCHAR" />
  10 + </resultMap>
  11 + <sql id="Base_Column_List" >
  12 + PERMISSION_ID, PERMISSION_NAME, PERMISSION_SIGN, DESCRIPTION, GROUP_NAME
  13 + </sql>
  14 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.math.BigDecimal" >
  15 + select
  16 + <include refid="Base_Column_List" />
  17 + from PERMISSION
  18 + where PERMISSION_ID = #{permissionId,jdbcType=DECIMAL}
  19 + </select>
  20 + <select id="findAll" resultMap="BaseResultMap" >
  21 + select
  22 + <include refid="Base_Column_List" />
  23 + from PERMISSION
  24 + </select>
  25 + <select id="findByUserId" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
  26 + select p.*
  27 + from USERS u
  28 + LEFT JOIN USER_ROLE ur on u.user_id= ur.user_id
  29 + LEFT JOIN ROLE r on ur.role_id=r.role_id
  30 + LEFT JOIN ROLE_PERMISSION rp on rp.role_id=r.role_id
  31 + LEFT JOIN PERMISSION p on p.PERMISSION_ID =rp.permission_id
  32 + where u.USER_ID = #{userId,jdbcType=DECIMAL}
  33 + </select>
  34 +
  35 + <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
  36 + delete from PERMISSION
  37 + where PERMISSION_ID = #{permissionId,jdbcType=DECIMAL}
  38 + </delete>
  39 + <insert id="insert" parameterType="com.tianbo.warehouse.model.PERMISSION" >
  40 + insert into PERMISSION (PERMISSION_ID, PERMISSION_NAME, PERMISSION_SIGN,
  41 + DESCRIPTION, GROUP_NAME)
  42 + values (#{permissionId,jdbcType=DECIMAL}, #{permissionName,jdbcType=VARCHAR}, #{permissionSign,jdbcType=VARCHAR},
  43 + #{description,jdbcType=VARCHAR}, #{groupName,jdbcType=VARCHAR})
  44 + </insert>
  45 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.PERMISSION" >
  46 + insert into PERMISSION
  47 + <trim prefix="(" suffix=")" suffixOverrides="," >
  48 + <if test="permissionId != null" >
  49 + PERMISSION_ID,
  50 + </if>
  51 + <if test="permissionName != null" >
  52 + PERMISSION_NAME,
  53 + </if>
  54 + <if test="permissionSign != null" >
  55 + PERMISSION_SIGN,
  56 + </if>
  57 + <if test="description != null" >
  58 + DESCRIPTION,
  59 + </if>
  60 + <if test="groupName != null" >
  61 + GROUP_NAME,
  62 + </if>
  63 + </trim>
  64 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  65 + <if test="permissionId != null" >
  66 + #{permissionId,jdbcType=DECIMAL},
  67 + </if>
  68 + <if test="permissionName != null" >
  69 + #{permissionName,jdbcType=VARCHAR},
  70 + </if>
  71 + <if test="permissionSign != null" >
  72 + #{permissionSign,jdbcType=VARCHAR},
  73 + </if>
  74 + <if test="description != null" >
  75 + #{description,jdbcType=VARCHAR},
  76 + </if>
  77 + <if test="groupName != null" >
  78 + #{groupName,jdbcType=VARCHAR},
  79 + </if>
  80 + </trim>
  81 + </insert>
  82 + <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.PERMISSION" >
  83 + update PERMISSION
  84 + <set >
  85 + <if test="permissionName != null" >
  86 + PERMISSION_NAME = #{permissionName,jdbcType=VARCHAR},
  87 + </if>
  88 + <if test="permissionSign != null" >
  89 + PERMISSION_SIGN = #{permissionSign,jdbcType=VARCHAR},
  90 + </if>
  91 + <if test="description != null" >
  92 + DESCRIPTION = #{description,jdbcType=VARCHAR},
  93 + </if>
  94 + <if test="groupName != null" >
  95 + GROUP_NAME = #{groupName,jdbcType=VARCHAR},
  96 + </if>
  97 + </set>
  98 + where PERMISSION_ID = #{permissionId,jdbcType=DECIMAL}
  99 + </update>
  100 + <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.PERMISSION" >
  101 + update PERMISSION
  102 + set PERMISSION_NAME = #{permissionName,jdbcType=VARCHAR},
  103 + PERMISSION_SIGN = #{permissionSign,jdbcType=VARCHAR},
  104 + DESCRIPTION = #{description,jdbcType=VARCHAR},
  105 + GROUP_NAME = #{groupName,jdbcType=VARCHAR}
  106 + where PERMISSION_ID = #{permissionId,jdbcType=DECIMAL}
  107 + </update>
  108 +</mapper>
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.tianbo.warehouse.dao.ROLEMapper" >
  4 + <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.ROLE" >
  5 + <id column="ROLE_ID" property="roleId" jdbcType="DECIMAL" />
  6 + <result column="ROLE_NAME" property="roleName" jdbcType="VARCHAR" />
  7 + <result column="ROLE_SIGN" property="roleSign" jdbcType="VARCHAR" />
  8 + <result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
  9 + </resultMap>
  10 + <sql id="Base_Column_List" >
  11 + ROLE_ID, ROLE_NAME, ROLE_SIGN, DESCRIPTION
  12 + </sql>
  13 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.math.BigDecimal" >
  14 + select
  15 + <include refid="Base_Column_List" />
  16 + from ROLE
  17 + where ROLE_ID = #{roleId,jdbcType=DECIMAL}
  18 + </select>
  19 + <select id="findRolesByUserId" parameterType="java.math.BigDecimal" resultMap="BaseResultMap">
  20 + SELECT
  21 + R.*
  22 + FROM
  23 + USERS U
  24 + LEFT JOIN USER_ROLE UR ON U .USER_ID = UR.USER_ID
  25 + LEFT JOIN ROLE R ON R.ROLE_ID= UR.ROLE_ID
  26 + where U.USER_ID = #{userId,jdbcType=DECIMAL}
  27 + </select>
  28 + <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
  29 + delete from ROLE
  30 + where ROLE_ID = #{roleId,jdbcType=DECIMAL}
  31 + </delete>
  32 + <insert id="insert" parameterType="com.tianbo.warehouse.model.ROLE" >
  33 + insert into ROLE (ROLE_ID, ROLE_NAME, ROLE_SIGN,
  34 + DESCRIPTION)
  35 + values (#{roleId,jdbcType=DECIMAL}, #{roleName,jdbcType=VARCHAR}, #{roleSign,jdbcType=VARCHAR},
  36 + #{description,jdbcType=VARCHAR})
  37 + </insert>
  38 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.ROLE" >
  39 + insert into ROLE
  40 + <trim prefix="(" suffix=")" suffixOverrides="," >
  41 + <if test="roleId != null" >
  42 + ROLE_ID,
  43 + </if>
  44 + <if test="roleName != null" >
  45 + ROLE_NAME,
  46 + </if>
  47 + <if test="roleSign != null" >
  48 + ROLE_SIGN,
  49 + </if>
  50 + <if test="description != null" >
  51 + DESCRIPTION,
  52 + </if>
  53 + </trim>
  54 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  55 + <if test="roleId != null" >
  56 + #{roleId,jdbcType=DECIMAL},
  57 + </if>
  58 + <if test="roleName != null" >
  59 + #{roleName,jdbcType=VARCHAR},
  60 + </if>
  61 + <if test="roleSign != null" >
  62 + #{roleSign,jdbcType=VARCHAR},
  63 + </if>
  64 + <if test="description != null" >
  65 + #{description,jdbcType=VARCHAR},
  66 + </if>
  67 + </trim>
  68 + </insert>
  69 + <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.ROLE" >
  70 + update ROLE
  71 + <set >
  72 + <if test="roleName != null" >
  73 + ROLE_NAME = #{roleName,jdbcType=VARCHAR},
  74 + </if>
  75 + <if test="roleSign != null" >
  76 + ROLE_SIGN = #{roleSign,jdbcType=VARCHAR},
  77 + </if>
  78 + <if test="description != null" >
  79 + DESCRIPTION = #{description,jdbcType=VARCHAR},
  80 + </if>
  81 + </set>
  82 + where ROLE_ID = #{roleId,jdbcType=DECIMAL}
  83 + </update>
  84 + <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.ROLE" >
  85 + update ROLE
  86 + set ROLE_NAME = #{roleName,jdbcType=VARCHAR},
  87 + ROLE_SIGN = #{roleSign,jdbcType=VARCHAR},
  88 + DESCRIPTION = #{description,jdbcType=VARCHAR}
  89 + where ROLE_ID = #{roleId,jdbcType=DECIMAL}
  90 + </update>
  91 +</mapper>
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.tianbo.warehouse.dao.USERSMapper" >
  4 + <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.USERS" >
  5 + <id column="USER_ID" property="userId" jdbcType="DECIMAL" />
  6 + <result column="USERNAME" property="username" jdbcType="VARCHAR" />
  7 + <result column="PASSWORD" property="password" jdbcType="VARCHAR" />
  8 + <result column="STATE" property="state" jdbcType="DECIMAL" />
  9 + <result column="CREATTIME" property="creattime" jdbcType="TIMESTAMP" />
  10 + <result column="SEX" property="sex" jdbcType="VARCHAR" />
  11 + <result column="ADDRESS" property="address" jdbcType="VARCHAR" />
  12 + <result column="BIRTHDAY" property="birthday" jdbcType="TIMESTAMP" />
  13 + <result column="MOBILEPHONE" property="mobilephone" jdbcType="VARCHAR" />
  14 + <result column="UPDATETIME" property="updatetime" jdbcType="TIMESTAMP" />
  15 + </resultMap>
  16 + <sql id="Base_Column_List" >
  17 + USER_ID, USERNAME, PASSWORD, STATE, CREATTIME, SEX, ADDRESS, BIRTHDAY, MOBILEPHONE,
  18 + UPDATETIME
  19 + </sql>
  20 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.math.BigDecimal" >
  21 + select
  22 + <include refid="Base_Column_List" />
  23 + from USERS
  24 + where USER_ID = #{userId,jdbcType=DECIMAL}
  25 + </select>
  26 + <select id="selectByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" >
  27 + select
  28 + <include refid="Base_Column_List" />
  29 + from USERS
  30 + where USERNAME = #{username,jdbcType=VARCHAR}
  31 + </select>
  32 + <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
  33 + delete from USERS
  34 + where USER_ID = #{userId,jdbcType=DECIMAL}
  35 + </delete>
  36 + <insert id="insert" parameterType="com.tianbo.warehouse.model.USERS" >
  37 + insert into USERS (USER_ID, USERNAME, PASSWORD,
  38 + STATE, CREATTIME, SEX,
  39 + ADDRESS, BIRTHDAY, MOBILEPHONE,
  40 + UPDATETIME)
  41 + values (#{userId,jdbcType=DECIMAL}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  42 + #{state,jdbcType=DECIMAL}, #{creattime,jdbcType=TIMESTAMP}, #{sex,jdbcType=VARCHAR},
  43 + #{address,jdbcType=VARCHAR}, #{birthday,jdbcType=TIMESTAMP}, #{mobilephone,jdbcType=VARCHAR},
  44 + #{updatetime,jdbcType=TIMESTAMP})
  45 + </insert>
  46 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.USERS" >
  47 + insert into USERS
  48 + <trim prefix="(" suffix=")" suffixOverrides="," >
  49 + <if test="userId != null" >
  50 + USER_ID,
  51 + </if>
  52 + <if test="username != null" >
  53 + USERNAME,
  54 + </if>
  55 + <if test="password != null" >
  56 + PASSWORD,
  57 + </if>
  58 + <if test="state != null" >
  59 + STATE,
  60 + </if>
  61 + <if test="creattime != null" >
  62 + CREATTIME,
  63 + </if>
  64 + <if test="sex != null" >
  65 + SEX,
  66 + </if>
  67 + <if test="address != null" >
  68 + ADDRESS,
  69 + </if>
  70 + <if test="birthday != null" >
  71 + BIRTHDAY,
  72 + </if>
  73 + <if test="mobilephone != null" >
  74 + MOBILEPHONE,
  75 + </if>
  76 + <if test="updatetime != null" >
  77 + UPDATETIME,
  78 + </if>
  79 + </trim>
  80 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  81 + <if test="userId != null" >
  82 + #{userId,jdbcType=DECIMAL},
  83 + </if>
  84 + <if test="username != null" >
  85 + #{username,jdbcType=VARCHAR},
  86 + </if>
  87 + <if test="password != null" >
  88 + #{password,jdbcType=VARCHAR},
  89 + </if>
  90 + <if test="state != null" >
  91 + #{state,jdbcType=DECIMAL},
  92 + </if>
  93 + <if test="creattime != null" >
  94 + #{creattime,jdbcType=TIMESTAMP},
  95 + </if>
  96 + <if test="sex != null" >
  97 + #{sex,jdbcType=VARCHAR},
  98 + </if>
  99 + <if test="address != null" >
  100 + #{address,jdbcType=VARCHAR},
  101 + </if>
  102 + <if test="birthday != null" >
  103 + #{birthday,jdbcType=TIMESTAMP},
  104 + </if>
  105 + <if test="mobilephone != null" >
  106 + #{mobilephone,jdbcType=VARCHAR},
  107 + </if>
  108 + <if test="updatetime != null" >
  109 + #{updatetime,jdbcType=TIMESTAMP},
  110 + </if>
  111 + </trim>
  112 + </insert>
  113 + <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.USERS" >
  114 + update USERS
  115 + <set >
  116 + <if test="username != null" >
  117 + USERNAME = #{username,jdbcType=VARCHAR},
  118 + </if>
  119 + <if test="password != null" >
  120 + PASSWORD = #{password,jdbcType=VARCHAR},
  121 + </if>
  122 + <if test="state != null" >
  123 + STATE = #{state,jdbcType=DECIMAL},
  124 + </if>
  125 + <if test="creattime != null" >
  126 + CREATTIME = #{creattime,jdbcType=TIMESTAMP},
  127 + </if>
  128 + <if test="sex != null" >
  129 + SEX = #{sex,jdbcType=VARCHAR},
  130 + </if>
  131 + <if test="address != null" >
  132 + ADDRESS = #{address,jdbcType=VARCHAR},
  133 + </if>
  134 + <if test="birthday != null" >
  135 + BIRTHDAY = #{birthday,jdbcType=TIMESTAMP},
  136 + </if>
  137 + <if test="mobilephone != null" >
  138 + MOBILEPHONE = #{mobilephone,jdbcType=VARCHAR},
  139 + </if>
  140 + <if test="updatetime != null" >
  141 + UPDATETIME = #{updatetime,jdbcType=TIMESTAMP},
  142 + </if>
  143 + </set>
  144 + where USER_ID = #{userId,jdbcType=DECIMAL}
  145 + </update>
  146 + <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.USERS" >
  147 + update USERS
  148 + set USERNAME = #{username,jdbcType=VARCHAR},
  149 + PASSWORD = #{password,jdbcType=VARCHAR},
  150 + STATE = #{state,jdbcType=DECIMAL},
  151 + CREATTIME = #{creattime,jdbcType=TIMESTAMP},
  152 + SEX = #{sex,jdbcType=VARCHAR},
  153 + ADDRESS = #{address,jdbcType=VARCHAR},
  154 + BIRTHDAY = #{birthday,jdbcType=TIMESTAMP},
  155 + MOBILEPHONE = #{mobilephone,jdbcType=VARCHAR},
  156 + UPDATETIME = #{updatetime,jdbcType=TIMESTAMP}
  157 + where USER_ID = #{userId,jdbcType=DECIMAL}
  158 + </update>
  159 +</mapper>