今天主要记录ssm中出现的一些问题与解决
1.认证与授权
在porm.xml文件中假如下面两段内容:
<spring.security.version>5.0.1.RELEASE</spring.security.version>
声明版本
<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.一些问题
Form的提交,你就会发现,一旦用户点击“Submit”按钮,表单开始提交,浏览器就会刷新页面,然后在新页面里告诉你操作是成功了还是失败了。如果不幸由于网络太慢或者其他原因,就会得到一个404页面。这就是Web的运作原理:一次HTTP请求对应一个页面。
如果要让用户留在当前页面中,同时发出新的HTTP请求,就必须用JavaScript发送这个新请求,接收到数据后,再用JavaScript更新页面,这样一来,用户就感觉自己仍然停留在当前页面,但是数据却可以不断地更新。
3.过滤器详细细节
3.1 配置文件
- 在web.xml中添加spring-security.xml的路径配置到里面
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml, classpath*:spring-security.xml</param-value>
</context-param>
3.2 实现
- IRoleDao中声明函数来返回查找到的ID
List<Role> findRoleByUserId(int id);
- RoleMapper文件中添加查询语句
<select id="findRoleByUserId" parameterType="int" resultType="com.zhongruan.bean.Role">
select * from role where id in (select roleId from users_role where userId = #{userId})
</select>
- UserInfoController.java原本跳转语句删除
/*@RequestMapping("login.do")
public ModelAndView checkLogin(UserInfo userInfo){
ModelAndView modelAndView = new ModelAndView();
if(userInfo.getUsername() == null){
modelAndView.setViewName("../index");
return modelAndView;
}else{
boolean flag;
flag = iUserInfoService.checkLogin(userInfo.getUsername(), userInfo.getPassword());
if (flag == true){
modelAndView.addObject("userInfo", userInfo);
modelAndView.setViewName("main");
//request.getSession().setAttribute("userinfo", userInfo);
return modelAndView;
}
modelAndView.setViewName("../failer");
return modelAndView;
}
}*/
- UserInfoService继承自UserDetailsService
public interface IUserInfoService extends UserDetailsService {
...
}
- 实现数据相关操作,在UserInfoServiceImpl.java中
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = iUserInfoDao.findUserByUsername(username);
List<Role> roles = iRoleDao.findRoleByUserId(userInfo.getId());
userInfo.setRoles(roles);
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;
}
- 修改aside.xml文件,满足相关条件
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
- 修改用户管理标签
<li id="system-setting">
<security:authorize access="hasRole('ADMIN')">
<a
href="${pageContext.request.contextPath}/user/findAll.do?pages=1&size=5"> <i
class="fa fa-circle-o"></i> 用户管理
</a>
</security:authorize>
</li>