权限

2019/7/22 10:16:54 人评论 次浏览 分类:学习教程

权限管理

今天进行权限的管理
首先导入spring包

   <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>${spring.security.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>${spring.security.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-core</artifactId>
      <version>${spring.security.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-taglibs</artifactId>
      <version>${spring.security.version}</version>
    </dependency>

添加依赖

    <param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>

新建表
在这里插入图片描述在这里插入图片描述
实现类

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo=userInfoDao.findByUserName(username);
        List<Role> roles= roleDao.findRoleByUserId(userInfo.getId());
		User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(roles));
		return user;
	}
	private Collection<?extends GrantedAuthority> getAuthority(List<Role> roles){
		List<SimpleGrantedAuthority> list=new ArrayList<>();
		for(Role role:roles){
			list.add(new SimpleGrantedAuthority("ROLE_"+role.getRolename()));
		}
		return  list;
	}
}

sql语句

<select id="findRoleByUserId" parameterType="int" resultType="com.zhongruan.bean.Role">
    select * from role where id in(select roleid from user_role where userid=#{id})
</select>
   <select id="findByUserName" parameterType="String" resultType="com.zhongruan.bean.UserInfo">
      select * from userinfo where username=#{username}
   </select>

登录
在这里插入图片描述
成功
在这里插入图片描述

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->