jsp 语言 JSP 整合 Shiro 实现权限管理示例

JSP阿木 发布于 2025-06-26 6 次阅读


JSP 整合 Shiro 实现权限管理示例

随着互联网技术的不断发展,权限管理在Web应用中扮演着越来越重要的角色。Shiro是一个开源的安全框架,它提供了身份验证、授权、会话管理和加密等功能。本文将围绕JSP语言,结合Shiro框架,展示如何实现一个简单的权限管理示例。

环境准备

在开始之前,我们需要准备以下环境:

1. JDK 1.8及以上版本

2. Tomcat 8.5及以上版本

3. Maven 3.5及以上版本

4. Shiro 1.4.0及以上版本

项目结构

以下是项目的目录结构:


src


├── main


│ ├── java


│ │ └── com


│ │ └── example


│ │ └── shiro


│ │ ├── controller


│ │ │ └── LoginController.java


│ │ ├── model


│ │ │ └── User.java


│ │ ├── service


│ │ │ └── UserService.java


│ │ └── util


│ │ └── ShiroUtil.java


│ ├── resources


│ │ ├── application.properties


│ │ └── shiro.ini


│ └── webapp


│ ├── WEB-INF


│ │ ├── views


│ │ │ ├── index.jsp


│ │ │ └── login.jsp


│ │ └── web.xml


│ └── index.html


1. 创建User实体类

我们需要创建一个User实体类,用于存储用户信息。

java

package com.example.shiro.model;

public class User {


private String username;


private String password;


private String role;

// 省略getter和setter方法


}


2. 创建UserService接口及其实现类

接下来,我们需要创建一个UserService接口及其实现类,用于处理用户相关的业务逻辑。

java

package com.example.shiro.service;

public interface UserService {


User getUserByUsername(String username);


}

package com.example.shiro.service.impl;

import com.example.shiro.model.User;

public class UserServiceImpl implements UserService {


@Override


public User getUserByUsername(String username) {


// 模拟从数据库获取用户信息


if ("admin".equals(username)) {


return new User(username, "admin", "admin");


}


return null;


}


}


3. 创建ShiroUtil工具类

ShiroUtil工具类用于简化Shiro配置和操作。

java

package com.example.shiro.util;

import org.apache.shiro.SecurityUtils;


import org.apache.shiro.authc.UsernamePasswordToken;


import org.apache.shiro.subject.Subject;

public class ShiroUtil {


public static void login(String username, String password) {


UsernamePasswordToken token = new UsernamePasswordToken(username, password);


Subject subject = SecurityUtils.getSubject();


subject.login(token);


}

public static void logout() {


SecurityUtils.getSubject().logout();


}

public static boolean isLogin() {


return SecurityUtils.getSubject().isAuthenticated();


}

public static String getPrincipal() {


return (String) SecurityUtils.getSubject().getPrincipal();


}


}


4. 创建LoginController控制器

LoginController控制器用于处理登录请求。

java

package com.example.shiro.controller;

import com.example.shiro.model.User;


import com.example.shiro.service.UserService;


import com.example.shiro.util.ShiroUtil;


import org.apache.shiro.authz.annotation.RequiresAuthentication;


import org.apache.shiro.authz.annotation.RequiresGuest;


import org.apache.shiro.authz.annotation.RequiresRoles;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;


import org.springframework.ui.Model;


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


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

@Controller


public class LoginController {


@Autowired


private UserService userService;

@GetMapping("/login")


public String login() {


return "login";


}

@PostMapping("/login")


public String login(String username, String password, Model model) {


User user = userService.getUserByUsername(username);


if (user != null && user.getPassword().equals(password)) {


ShiroUtil.login(username, password);


return "redirect:index";


} else {


model.addAttribute("error", "用户名或密码错误!");


return "login";


}


}

@GetMapping("/index")


@RequiresAuthentication


public String index() {


return "index";


}

@GetMapping("/logout")


@RequiresAuthentication


public String logout() {


ShiroUtil.logout();


return "redirect:login";


}

@GetMapping("/admin")


@RequiresRoles("admin")


public String admin() {


return "admin";


}


}


5. 配置shiro.ini文件

在resources目录下创建shiro.ini文件,配置Shiro框架的权限管理。

ini

[main]


authc.loginUrl=/login


authc.unauthorizedUrl=/unauthorized


user.authc.userCacheName=userCache


user.authc.sessionMode=none


user.authc.cachingEnabled=true

[users]


admin=123,admin

[roles]


admin=


6. 配置web.xml

在webapp/WEB-INF目录下创建web.xml文件,配置Shiro过滤器。

xml

<filter>


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


<filter-class>org.apache.shiro.web.filter.mgt.DefaultFilter</filter-class>


</filter>


<filter-mapping>


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


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


</filter-mapping>


7. 创建JSP页面

在webapp/WEB-INF/views目录下创建index.jsp、login.jsp和unauthorized.jsp页面。

index.jsp:

jsp

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


<html>


<head>


<title>首页</title>


</head>


<body>


<h1>欢迎来到首页!</h1>


<a href="logout">退出</a>


</body>


</html>


login.jsp:

jsp

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


<html>


<head>


<title>登录</title>


</head>


<body>


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


<div>


<label for="username">用户名:</label>


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


</div>


<div>


<label for="password">密码:</label>


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


</div>


<div>


<input type="submit" value="登录">


</div>


</form>


<c:if test="${not empty error}">


<div style="color: red;">${error}</div>


</c:if>


</body>


</html>


unauthorized.jsp:

jsp

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


<html>


<head>


<title>未授权</title>


</head>


<body>


<h1>您没有权限访问该页面!</h1>


<a href="login">返回登录</a>


</body>


</html>


总结

本文通过JSP和Shiro框架,实现了一个简单的权限管理示例。在实际项目中,我们可以根据需求扩展Shiro的功能,如添加更多角色、权限、会话管理等。希望本文能对您有所帮助。