1. 数据库中的表结构
用户表(userinfo):
角色表(role):
用户和角色的关系表(user_role):
2. 添加SpringSecurity的依赖包
在项目的pom.xml中的properties标签中添加如下代码:
<spring.security.version>5.0.1.RELEASE</spring.security.version>
在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>
3. 在webapp/WEB-INF下的web.xml中添加过滤器(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>
4. 写springsecurity.xml 核心配置文件。
在resources路径下新建springsecurity.xml:
在springsecurity.xml中写入如下代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<security:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"></security:global-method-security>
<!-- 配置不拦截的资源 -->
<security:http pattern="/login.jsp" security="none"/>
<security:http pattern="/failer.jsp" security="none"/>
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/img/**" security="none"/>
<security:http pattern="/plugins/**" security="none"/>
<!--
配置具体的规则
auto-config="true" 不用自己编写登录的页面,框架提供默认登录页面
use-expressions="false" 是否使用SPEL表达式(没学习过)
-->
<security:http auto-config="true" use-expressions="true">
<!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
default-target-url="/index.jsp"
authentication-failure-url="/failer.jsp"
authentication-success-forward-url="/pages/main.jsp"/>
<!-- 关闭跨域请求 -->
<security:csrf disabled="true"/>
<!--退出并跳转到首页-->
<security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp"></security:logout>
</security:http>
<!-- 切换成数据库中的用户名和密码 -->
<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">
<!-- 配置加密的方式
<security:password-encoder ref="passwordEncoder"/> -->
</security:authentication-provider>
</security:authentication-manager>
<!-- 配置加密类 -->
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans>
5. 在webapp/WEB-INF下的web.xml 里面加载SpringSecurity.xml
下图中红色方框部分即为加载SpringSecurity.xml。
6. 使service层的接口类继承UserDetailsService类,并在其实现类userService中重写loadUserByUsername方法
如下代码为方法的具体实现,通过Dao层找到相应用户名的用户信息,再通过用户Id找到其所具有的所有权限,然后新建User对象(此处的User为org.springframework.security.core.userdetails.User)。
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = userDao.findByUserName(username);
if(userInfo != null) {
List<Role> roles = roleDao.findRoleByUserId(userInfo.getId());
userInfo.setRoles(roles);
User user = new User(userInfo.getUsername(), "{noop}" + userInfo.getPassword(), getAuthority(roles));
return user;
}
return null;
}
private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
List<SimpleGrantedAuthority> list = new ArrayList<SimpleGrantedAuthority>();
for (Role role : roles) {
list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
}
return list;
}
7. 改写jsp页面中的内容
- 在登录页面(login.jsp)中修改:原来点击登录按钮时是转到controller中执行,现改为到SpringSecurity.xml中执行,根据上方SpringSecurity.xml中的配置,将login.jsp中的form action路径改为${pageContext.request.contextPath}/login.do。
- 在需要添加权限的页面头部添加如下代码:
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
- 在设置权限的地方添加权限设置。例如,如果是管理员可以进行用户管理,在点击用户管理的地方添加security:authorize标签:
<security:authorize access="hasRole(ADMIN)">
<a
href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5"> <i
class="fa fa-circle-o"></i> 用户管理
</a>
</security:authorize>
这样普通用户进入页面时即看不见该选项,实现了管理员和普通用户的权限管理。