<file_sep>package com.pyg.springsecurity.service.impl;

import com.pyg.springsecurity.mapper.SysMenuMapper;
import com.pyg.springsecurity.service.ISysMenuService;
import com.pyg.springsecurity.entity.SysMenu;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

}
<file_sep>package com.pyg.springsecurity.mapper;

import com.pyg.springsecurity.entity.SysUserRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    SysUserRole selectByUserId(Integer userId);
}
<file_sep>package com.pyg.springsecurity.service;

import com.pyg.springsecurity.entity.SysUser;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * <p>
 * 系统用户 服务类
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface ISysUserService extends IService<SysUser> {

}
<file_sep>package com.pyg.springsecurty;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @ClassName test
 * @Description TODO
 * @Date 2020/1/3 下午 06:25
 * @Version 1.0
 **/
public class test {
    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("123");
        System.out.println(encode);
    }
}
<file_sep>package com.pyg.springsecurity.mapper;

import com.pyg.springsecurity.entity.SysMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

}
<file_sep>package com.pyg.springsecurity.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pyg.springsecurity.entity.SysRoleMenu;
import com.pyg.springsecurity.entity.SysUser;
import com.pyg.springsecurity.entity.SysUserRole;
import com.pyg.springsecurity.mapper.SysRoleMenuMapper;
import com.pyg.springsecurity.mapper.SysUserMapper;
import com.pyg.springsecurity.mapper.SysUserRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class UserDetailsServiceImpl implements UserDetailsService{

	@Resource
	SysUserMapper userMapper;
	@Resource
	SysUserRoleMapper userRoleMapper;
	@Resource
	SysRoleMenuMapper roleMenuMapper;

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		logger.debug("===============用户名："+username);

		SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername,username));

		if(user == null){
			throw new UsernameNotFoundException("用户[" + username + "]不存在");
		}

		//获取角色
		SysUserRole userRole = userRoleMapper.selectByUserId(user.getId());
		//获取菜单权限

		List<String> menuCodeList = new ArrayList<String>();

		if(userRole != null){
			List<SysRoleMenu> roleMenuList = roleMenuMapper.selectByRoleId(userRole.getRoleId());

			menuCodeList = roleMenuList.stream().map(roleMenu -> roleMenu.getMenuCode()).collect(Collectors.toList());
		}else{
			menuCodeList.add("none_menu");
		}

		return new User(username, user.getPassword(), user.getEnabled(), true, true, user.getAccountNonLocked(), AuthorityUtils.createAuthorityList(menuCodeList.toArray(new String[menuCodeList.size()])));


	}

}
<file_sep>package com.pyg.springsecurity.mapper;

import com.pyg.springsecurity.entity.SysRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

}
<file_sep>package com.pyg.springsecurity.service.impl;

import com.pyg.springsecurity.entity.SysUser;
import com.pyg.springsecurity.service.ISysUserService;
import com.pyg.springsecurity.mapper.SysUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


}
<file_sep>package com.pyg.springsecurity.mapper;

import com.pyg.springsecurity.entity.SysRoleMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {

    List<SysRoleMenu> selectByRoleId(Integer roleId);
}
<file_sep>package com.pyg.springsecurity.mapper;

import com.pyg.springsecurity.entity.SysUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 * 系统用户 Mapper 接口
 * </p>
 *
 * @author zouYunHao
 * @since 2020-01-02
 */
public interface SysUserMapper extends BaseMapper<SysUser> {

    SysUser selectByUsername(String username);
}
