利用ssm框架写网页之三

it2022-05-05  100

添加权限

1、数据库处理2、修改pox.xml3、编写代码4、导入资源文件5、运行结果 本博客是在我的上一篇博客《利用ssm框架写网页之二》的基础上进行,建立项目等工作在这里就不再做详细介绍,详情请看我的上一篇博客。

1、数据库处理

建立在昨天的数据库上进行建表,并添加数据,建表以及数据如图: role: userinfo_role: userinfo:

2、修改pox.xml

将以下内容导入pox.xml文件 版本号:

<spring.security.version>5.0.1.RELEASE</spring.security.version>

dependency:

<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、编写代码

在bean文件夹下建立Role文件,输入如下代码:

package com.zhongruan.bean; public class Role { private int id; private String rolename; private String roleDesc; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getRolename() { return rolename; } public void setRolename(String rolename) { this.rolename = rolename; } public String getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } @Override public String toString() { return "Role{" + "id=" + id + ", rolename='" + rolename + '\'' + ", roleDesc='" + roleDesc + '\'' + '}'; } }

UserInfo:

package com.zhongruan.bean; public class UserInfo { public UserInfo(){ } public UserInfo(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserInfo{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }

UserInfoController:

package com.zhongruan.controller; import com.github.pagehelper.PageInfo; import com.zhongruan.bean.UserInfo; import com.zhongruan.service.IUserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.List; @Controller @RequestMapping("user") public class UserInfoController { @Autowired private IUserInfoService userInfoService; @RequestMapping("/findAll.do") public ModelAndView findAll(@RequestParam(defaultValue= "1")int page,@RequestParam(defaultValue = "5")int size){ ModelAndView mv=new ModelAndView(); List<UserInfo> userInfos=userInfoService.findAll(page,size); PageInfo pageInfo=new PageInfo(userInfos); mv.addObject("pageInfo",pageInfo); mv.setViewName("user-list"); return mv; } /* @RequestMapping("login.do") public ModelAndView login(UserInfo userInfo){ ModelAndView mv=new ModelAndView(); if(null==userInfo.getUsername()){ mv.setViewName("main"); }else{ boolean flag=userInfoService.login(userInfo); if(flag){ mv.setViewName("main"); }else { mv.setViewName("../failer"); } } return mv; } */ @RequestMapping("toUpdate.do") public ModelAndView toUpdate(int id){ ModelAndView mv=new ModelAndView(); UserInfo userInfo=userInfoService.selectById(id); mv.addObject("userInfo",userInfo); mv.setViewName("user-update"); return mv; } @RequestMapping("update.do") public String update(UserInfo userInfo){ userInfoService.update(userInfo); return "redirect:findAll.do"; } @RequestMapping("add.do") public String add(UserInfo userInfo){ userInfoService.add(userInfo); return "redirect:findAll.do"; } }

在dao文件夹下创建IRoleDao文件,编写代码如下:

package com.zhongruan.dao; import com.zhongruan.bean.Role; import java.util.List; public interface IRoleDao { List<Role> findRoleByUserId(int id); }

UserInfoServiceImpl:

package com.zhongruan.service.impl; import com.github.pagehelper.PageHelper; import com.zhongruan.bean.Role; import com.zhongruan.bean.UserInfo; import com.zhongruan.dao.IRoleDao; import com.zhongruan.dao.IUserInfoDao; import com.zhongruan.service.IUserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Collection; import java.util.List; @Service("userInfoService") public class UserInfoServiceImpl implements IUserInfoService { @Autowired private IUserInfoDao userInfoDao; @Autowired private IRoleDao roleDao; @Override public List<UserInfo> findAll(int page,int size) { PageHelper.startPage(page,size); return userInfoDao.findAll(); } @Override public boolean login(UserInfo userInfo) { UserInfo user=userInfoDao.login(userInfo); if(user!=null){ return true; }else{ return false; } } @Override public UserInfo selectById(int id) { UserInfo userInfo=userInfoDao.selectById(id); return userInfo; } @Override public void update(UserInfo userInfo) { userInfoDao.update(userInfo); } @Override public void add(UserInfo userInfo) { userInfoDao.add(userInfo); } @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; } }

IUserInfoService:

package com.zhongruan.service; import com.zhongruan.bean.UserInfo; import org.springframework.security.core.userdetails.UserDetailsService; import java.util.List; public interface IUserInfoService extends UserDetailsService { public List<UserInfo> findAll(int page,int size); public boolean login(UserInfo userInfo); UserInfo selectById(int id); void update(UserInfo userInfo); void add(UserInfo userInfo); }

4、导入资源文件

方法与上次一样,导入资源文件就可以了,其中数据库方面根据自己的数据库命名来修改,废话不多说,直接看百度云资源 链接:https://pan.baidu.com/s/115ChoPhovAK72chFpBgb4A 提取码:ppvo

5、运行结果

如果你已经做完了以上步骤,那么你的权限功能已经完成了,让我们来看一下运行结果。 如有有什么问题,欢迎在评论区留言。


最新回复(0)