1.添加整合mybatis的依赖
org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 com.alibaba druid-spring-boot-starter1.1.9 mysql mysql-connector-javaorg.projectlombok lombok1.18.20 provided
2.配置yml文件
spring: freemarker: settings: classic_compatible: true #处理空值 datetime_format: yyy-MM-dd HH:mm number_format: 0.## suffix: .ftl template-loader-path: - classpath:/templates datasource: name: test url: jdbc:mysql://localhost:3306/mayikt_rbac?serverTimezone=UTC&useSSL=true&useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 # druid 连接池 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver
3.创建相应的类与包
permissionEntity
public interface PermissionMapper { @Select(" select * from sys_permission ") ListfindAllPermission(); }
userMapper
public interface UserMapper { @Select(" select * from sys_user where username = #{userName}") UserEntity findByUsername(@Param("userName") String userName); @Select(" select permission.* from sys_user user" + " inner join sys_user_role user_role" + " on user.id = user_role.user_id inner join " + "sys_role_permission role_permission on user_role.role_id = role_permission.role_id " + " inner join sys_permission permission on role_permission.perm_id = permission.id where user.username = #{userName};") ListfindPermissionByUsername(@Param("userName") String userName); }
4.修改权限规则
@Override protected void configure(HttpSecurity http) throws Exception { // //配置认证方式 token,设置httpBasic模式 // //拦截全部请求进入httpBasic模式 http.authorizeRequests().antMatchers(" auth.inMemoryAuthentication().withUser("mubai").password("123456").authorities("/"); //此处的意思是 mubai这个账号可以访问所有的接口 // auth.inMemoryAuthentication().withUser("mubai_admin").password("123456").authorities("/"); // auth.inMemoryAuthentication().withUser("mubai_crud").password("123456").authorities("addMember","updateMember","delMember"); // auth.inMemoryAuthentication().withUser("mubai_show").password("123456").authorities("showMember"); auth.userDetailsService(memberDetailsService).passwordEncoder(new PasswordEncoder() { //对密码进行md5加盐加密 @Override public String encode(CharSequence rawPassword) { return MD5Util.encode((String) rawPassword); } @Override public boolean matches(CharSequence rawPassword, String encodePassword) { // md5传递密码,传递密码MD5加密==DB中的密码则输入正确 String rawPass = MD5Util.encode((String) rawPassword); boolean result = rawPass.equals(encodePassword); return result; } }); }
启动登录测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)