概述
权限管理可以让不同角色拥有不同的权限,没有相应的权限就无法访问相关信息,从而有利于信息的保护。在这个项目中,利用权限管理可以实现游客(未登录的用户或没有权限的用户)不能访问系统主页,输入主页链接会跳转到登录页;利用权限管理还可以实现不同角色的用户可以访问的页面不同化,管理员用户可以访问所有页面,而普通用户不能访问用户管理页面。
开发环境
- IDEA2018.2.2
- tomcat7.0.94
- JDK1.8
- MySQL5.5.58
- Maven3.6.0
开发前提
已有所需工程项目及前端页面文件等
开发步骤
0.建立数据库user,编写相关实体类、持久类、服务类、控制类及XXXMapper.xml代码(相关代码略):
userinfo
(
id
int(10) NOT NULL AUTO_INCREMENT,
username
varchar(50) DEFAULT NULL,
password
varchar(50) DEFAULT NULL,
PRIMARY KEY (id
)
)
role
(
id
int(11) NOT NULL AUTO_INCREMENT,
roleName
varchar(255) DEFAULT NULL,
roleDesc
varchar(255) DEFAULT NULL,
PRIMARY KEY (id
)
)
user_role
(
userId
int(11) DEFAULT NULL,
roleId
int(11) DEFAULT NULL,
KEY userid
(userId
),
KEY roleid
(roleId
),
CONSTRAINT roleid
FOREIGN KEY (roleid
) REFERENCES role
(id
),
CONSTRAINT userid
FOREIGN KEY (userid
) REFERENCES userinfo
(id
)
)
1.导包:在pom.xml文件中加入以下代码:
(1)在<properties>标签里添加springsecurity版本号代码:
<spring.security.version>5.0.1.RELEASE</spring.security.version>
(2)在<dependencies>标签里添加如下代码:
<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>
2.修改web.xml:
(1)修改<context-param>标签里<param-value>的值,修改后代码如下:
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
</context-param>
(2)添加一个filter:
<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.让IUserInfoService接口继承一个父类UserDetailsService,并在UserInfoServiceImpl类中实现loadUserByUsername()方法,相关代码如下:
//UserInfoServiceImpl类
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = userInfoDao.findByUserName(username);
List<Role> roles = roleDao.selectByUserId(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;
}
4.将UserInfoController类中的login()函数注释掉;
5.将login.jsp文件中登录按钮的跳转链接修改为${pageContext.request.contextPath}/login.do;
6.至此,权限管理之游客无法访问系统主页已经实现。