jsp 语言 JSP 整合 Shiro 实现角色权限分配示例

JSP阿木 发布于 2025-07-03 14 次阅读


JSP 整合 Shiro 实现角色权限分配示例

JavaServer Pages(JSP)是一种动态网页技术,它允许开发人员使用Java代码来创建动态网页。Shiro 是一个开源的安全框架,用于实现身份验证、授权、会话管理和加密等功能。本文将介绍如何使用 JSP 和 Shiro 实现角色权限分配,通过一个简单的示例来展示如何将 Shiro 集成到 JSP 应用中。

环境准备

在开始之前,请确保以下环境已经准备就绪:

1. Java Development Kit (JDK) 1.8 或更高版本

2. Apache Maven 3.6 或更高版本

3. Tomcat 9.0 或更高版本

4. Shiro 1.4.0 或更高版本

创建项目

1. 使用 Maven 创建一个 Web 项目。

2. 在 `pom.xml` 文件中添加 Shiro 和 JSP 相关的依赖。

xml

<dependencies>


<!-- Shiro 核心依赖 -->


<dependency>


<groupId>org.apache.shiro</groupId>


<artifactId>shiro-core</artifactId>


<version>1.4.0</version>


</dependency>


<!-- Shiro Web 依赖 -->


<dependency>


<groupId>org.apache.shiro</groupId>


<artifactId>shiro-web</artifactId>


<version>1.4.0</version>


</dependency>


<!-- JSP 标准库依赖 -->


<dependency>


<groupId>javax.servlet.jsp</groupId>


<artifactId>javax.servlet.jsp-api</artifactId>


<version>2.3.3</version>


<scope>provided</scope>


</dependency>


<!-- JSTL 标准库依赖 -->


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>jstl</artifactId>


<version>1.2</version>


</dependency>


<!-- Servlet API 依赖 -->


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>javax.servlet-api</artifactId>


<version>4.0.1</version>


<scope>provided</scope>


</dependency>


</dependencies>


配置 Shiro

1. 在 `src/main/webapp/WEB-INF` 目录下创建一个名为 `shiro.ini` 的文件,用于配置 Shiro。

2. 在 `shiro.ini` 文件中定义用户、角色和权限。

ini

[users]


admin = admin, user


user = user, guest

[roles]


admin = admin


user = guest

[permissions]


admin = /admin/, /user/, /guest/, /logout


user = /user/, /guest/, /logout


guest = /guest/, /logout


3. 在 `src/main/webapp/WEB-INF/web.xml` 文件中配置 Shiro Filter。

xml

<filter>


<filter-name>shiroFilter</filter-name>


<filter-class>org.apache.shiro.web.filter.servlet.ShiroFilter</filter-class>


<init-param>


<param-name>loginUrl</param-name>


<param-value>/login.jsp</param-value>


</init-param>


<init-param>


<param-name>unauthorizedUrl</param-name>


<param-value>/unauthorized.jsp</param-value>


</init-param>


<filter-mapping>


<filter-name>shiroFilter</filter-name>


<url-pattern>/</url-pattern>


</filter-mapping>


</filter>


创建 JSP 页面

1. 创建一个名为 `login.jsp` 的登录页面。

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>


<html>


<head>


<title>Login</title>


</head>


<body>


<form action="login" method="post">


<label for="username">Username:</label>


<input type="text" id="username" name="username" required>


<label for="password">Password:</label>


<input type="password" id="password" name="password" required>


<input type="submit" value="Login">


</form>


</body>


</html>


2. 创建一个名为 `unauthorized.jsp` 的未授权页面。

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>


<html>


<head>


<title>Unauthorized</title>


</head>


<body>


<h1>Unauthorized Access!</h1>


<p>You are not authorized to access this page.</p>


</body>


</html>


3. 创建一个名为 `index.jsp` 的首页。

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags-shiro"%>


<!DOCTYPE html>


<html>


<head>


<title>Home</title>


</head>


<body>


<h1>Welcome to the Home Page!</h1>


<shiro:hasRole name="admin">


<a href="/admin">Admin Page</a>


</shiro:hasRole>


<shiro:hasRole name="user">


<a href="/user">User Page</a>


</shiro:hasRole>


<shiro:hasRole name="guest">


<a href="/guest">Guest Page</a>


</shiro:hasRole>


<a href="logout">Logout</a>


</body>


</html>


编写控制器

1. 在 `src/main/java` 目录下创建一个名为 `LoginController.java` 的控制器。

java

package com.example.controller;

import org.apache.shiro.SecurityUtils;


import org.apache.shiro.authc.UsernamePasswordToken;


import org.apache.shiro.subject.Subject;


import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RequestMethod;


import org.springframework.web.bind.annotation.RequestParam;

@Controller


public class LoginController {

@RequestMapping(value = "/login", method = RequestMethod.POST)


public String login(@RequestParam("username") String username,


@RequestParam("password") String password) {


Subject subject = SecurityUtils.getSubject();


UsernamePasswordToken token = new UsernamePasswordToken(username, password);


try {


subject.login(token);


return "redirect:index.jsp";


} catch (Exception e) {


return "redirect:login.jsp";


}


}


}


运行项目

1. 启动 Tomcat 服务器。

2. 打开浏览器,访问 `http://localhost:8080/your-app-name/login.jsp` 进行登录。

总结

本文介绍了如何使用 JSP 和 Shiro 实现角色权限分配。通过配置 Shiro Filter 和定义用户、角色和权限,我们可以轻松地实现基于角色的访问控制。在实际项目中,可以根据需求扩展 Shiro 的功能,例如集成数据库存储、缓存和加密等。

注意:本文提供的代码仅供参考,实际项目中可能需要根据具体需求进行调整。