java web权限控制的搭建步骤

权限控制是一个程序很重要的功能,它有两个作用:
(1)认证 判断用户名和密码是否正确。
(2)授权 一个用户是否有权利执行某些操作。
例如:管理员和用户的身份不同,看到的界面和可以执行的操作也应该不同。下面讲讲如何实现权限控制:
1.首先还是在pom中导入jar包

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

然后在properties中导入springSecurity版本号

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

2.在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>

3.写springsecurity.xml 核心配置文件
springsecurity.xml是放在web的resource包中的,用于控制逻辑。
 
4.在web.xml 里面加载springSecurity.xml
在这里插入图片描述
添加图中箭头的文件。
 
5.改写service层
将IUserService接口改为继承 UserDetailsService,然后再实现这个方法

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

其中,在IUserDao接口中声明 findUserByName方法:

 public  UserInfo findUserByName(String username);

然后在usermapper中实现:

 <select id="findUserByName" parameterType="String" resultType="com.****.bean.UserInfo">
        select * from userinfo where username=#{username}
    </select>

同样,为了得到用户身份,还需要创建Role结构体,里面的成员变量有:

 private int id;
    private String roleName;
    private String roleDesc;

创建它们的get、set和ToString方法。在UserInfo中新增private List<Role> roles;属性,添加get、set方法并修改ToString方法。
新建IRoleDao接口,添加findRoleByUserId方法:

List<Role> findRoleByUserId(int userId);

在rolemapper中实现:

  <select id="findRoleByUserId" parameterType="int" resultType="com.****.bean.Role">
        select * from role where id in (select roleId from users_role where userId=#{userId})
    </select>

getAuthority(roles)方法的实现为:

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

 
6.改写页面
修改点击登录的动作,改为:

action="${pageContext.request.contextPath}/login.do"

然后设置权限,用户看不到用户管理,方法是:

<security:authorize access="hasRole('ADMIN')">
						<a
						href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=4"> <i
							class="fa fa-circle-o"></i> 用户管理
					</a>
						</security:authorize>

这里hasRole中的ADMIN就是可以看到的角色。
使用时要在文件开头添加:

<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值