《中软国际实习Day8:基于分子模块的ssm框架实现javaweb的登录权限管理》

一、导jar包

  • 在ssm_parent下的pom.xml中导入所需jar包
<spring.security.version>5.0.1.RELEASE</spring.security.version> //添加进<properties>
 <dependency>                   //添加进<dependencies>
    <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>

二、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>

三、写springsecurity.xml 核心配置文件

  • 将已准备好的素材spring-security.xml导入web下的resources

四、web.xml 里面加载springSecurity.xml

  • 在以下代码中的路径处添加classpath*:spring-security.xml
<!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
  </context-param>

五、userService extend UserDetailsService

1、在com.zhongruan.bean下新建Role.java

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 + '\'' +
                '}';
    }
}

2、修改IUserService

package com.zhongruan.service;

import com.zhongruan.bean.UserInfo;
import org.springframework.security.core.userdetails.UserDetailsService;


import java.util.List;

public interface IUserService extends UserDetailsService{
    public List<UserInfo> findAll(int page,int size);

    public Boolean doLogin(UserInfo userInfo);

    public int updateUser(UserInfo userInfo);

    public UserInfo queryById(long id);

    public int delUser(long id);

    public int addUser(UserInfo userInfo);
}

3、修改UserService

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.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
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.List;

@Service("userService")
public class UserService implements IUserService {

    @Autowired
    private IUserDao userDao;
    @Autowired
    private IRoleDao roleDao;

    @Override
    public List<UserInfo> findAll(int page,int size) {
        PageHelper.startPage(page,size);
        return userDao.findAll();
    }

    @Override
    public Boolean doLogin(UserInfo userInfo) {
        UserInfo userInfo1 = userDao.doLogin(userInfo);
        if(null!=userInfo1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int updateUser(UserInfo userInfo){
        return userDao.updateUser(userInfo);
    }

    @Override
    public UserInfo queryById(long id) {
        return userDao.queryById(id);
    }

    @Override
    public int delUser(long id){
        return userDao.delUser(id);
    }

    @Override
    public int addUser(UserInfo userInfo){
        return userDao.addUser(userInfo);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo=userDao.findByUserName(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;
    }

    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;
    }
}


六、改写页面

在aside.jsp中以下位置处添加<security:authentication property=“principal.username”></security:authentication>

<div class="pull-left info">
		<security:authentication property="principal.username"></security:authentication>
		<a href="#"><i class="fa fa-circle text-success"></i> 在线</a>
</div>

七、页面效果图

(管理员登录有用户管理功能)
在这里插入图片描述
(普通用户登录不能有用户管理功能)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值