权限控制是一个程序很重要的功能,它有两个作用:
(1)认证 判断用户名和密码是否正确。
(2)授权 一个用户是否有权利执行某些操作。
例如:管理员和用户的身份不同,看到的界面和可以执行的操作也应该不同。下面讲讲如何实现权限控制:
1.首先还是在pom中导入jar包
<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>
然后在properties中导入springSecurity版本号
<spring.security.version>5.0.1.RELEASE</spring.security.version>
2.在web.xml 添加过滤器
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.写springsecurity.xml 核心配置文件
springsecurity.xml是放在web的resource包中的,用于控制逻辑。
4.在web.xml 里面加载springSecurity.xml
添加图中箭头的文件。
5.改写service层
将IUserService接口改为继承 UserDetailsService,然后再实现这个方法
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = userDao.findUserByName(username);
User user = null;
if (userInfo!=null){
List<Role> roles = roleDao.findRoleByUserId(userInfo.getId());
userInfo.setRoles(roles);
user =new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(roles));
}
return user;
}
其中,在IUserDao接口中声明 findUserByName方法:
public UserInfo findUserByName(String username);
然后在usermapper中实现:
<select id="findUserByName" parameterType="String" resultType="com.****.bean.UserInfo">
select * from userinfo where username=#{username}
</select>
同样,为了得到用户身份,还需要创建Role结构体,里面的成员变量有:
private int id;
private String roleName;
private String roleDesc;
创建它们的get、set和ToString方法。在UserInfo中新增private List<Role> roles;
属性,添加get、set方法并修改ToString方法。
新建IRoleDao接口,添加findRoleByUserId方法:
List<Role> findRoleByUserId(int userId);
在rolemapper中实现:
<select id="findRoleByUserId" parameterType="int" resultType="com.****.bean.Role">
select * from role where id in (select roleId from users_role where userId=#{userId})
</select>
getAuthority(roles)方法的实现为:
private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
List<SimpleGrantedAuthority> list = new ArrayList<>();
for (Role role:roles){
list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
}
return list;
}
6.改写页面
修改点击登录的动作,改为:
action="${pageContext.request.contextPath}/login.do"
然后设置权限,用户看不到用户管理,方法是:
<security:authorize access="hasRole('ADMIN')">
<a
href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=4"> <i
class="fa fa-circle-o"></i> 用户管理
</a>
</security:authorize>
这里hasRole中的ADMIN就是可以看到的角色。
使用时要在文件开头添加:
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>