String requestURL = getPathWithinApplication(request); System.out.println("请求的url :" + requestURL); Subject subject = SecurityUtils.getSubject(); if (!subject.isAuthenticated()) { // 如果没有登录, 进入登录流程 WebUtils.issueRedirect(request, response, "/login"); return false; }
时间: 2024-01-10 21:03:16 浏览: 33
这段代码是一个 Java 项目中集成 Shiro 框架的自定义拦截器代码,用于实现自定义的 URL 权限控制。具体来说,它首先获取当前请求的 URL,然后通过 `SecurityUtils.getSubject()` 方法获取当前用户的 `Subject` 对象。
接着,它判断当前用户是否已经登录,如果没有登录,则会通过 `WebUtils.issueRedirect()` 方法将用户重定向到登录页面,然后返回 `false`,表示不允许访问。
在 Shiro 中,拦截器用于对请求进行拦截和处理,比如进行身份认证、权限控制等。在这个自定义拦截器中,我们根据用户的登录状态,判断其是否有权访问当前请求的 URL。如果用户未登录,则会将其重定向到登录页面;如果用户已经登录,则会允许其访问该 URL。
需要注意的是,这个自定义拦截器并没有进行具体的权限控制,而是使用了 Shiro 默认的身份认证机制,即要求用户必须已经登录才能访问受保护的资源。如果需要进行更细粒度的权限控制,可以通过自定义的 `Realm` 对象实现。
相关问题
将package shiyan3; public class Test { public static void main(String[] args) { try { Student student = new Student("Alice", "123456", "Computer Science"); System.out.println("Student name: " + student.getUsername()); System.out.println("Student password: " + student.getPassword()); System.out.println("Student major: " + student.getMajor()); Teacher teacher = new Teacher("Bob", "123456", "Mathematics"); System.out.println("Teacher name: " + teacher.getUsername()); System.out.println("Teacher password: " + teacher.getPassword()); System.out.println("Teacher department: " + teacher.getDepartment()); Admin admin = new Admin("Charlie", "123456", "Director"); System.out.println("Admin name: " + admin.getUsername()); System.out.println("Admin password: " + admin.getPassword()); System.out.println("Admin position: " + admin.getPosition()); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } } }修改这段代码,使他有键盘输入的值
以下是修改后的代码,使用了Scanner类来获取键盘输入的值:
```java
package shiyan3;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String studentName = scanner.nextLine();
System.out.print("Enter student password: ");
String studentPassword = scanner.nextLine();
System.out.print("Enter student major: ");
String studentMajor = scanner.nextLine();
Student student = new Student(studentName, studentPassword, studentMajor);
System.out.println("Student name: " + student.getUsername());
System.out.println("Student password: " + student.getPassword());
System.out.println("Student major: " + student.getMajor());
System.out.print("\nEnter teacher name: ");
String teacherName = scanner.nextLine();
System.out.print("Enter teacher password: ");
String teacherPassword = scanner.nextLine();
System.out.print("Enter teacher department: ");
String teacherDepartment = scanner.nextLine();
Teacher teacher = new Teacher(teacherName, teacherPassword, teacherDepartment);
System.out.println("Teacher name: " + teacher.getUsername());
System.out.println("Teacher password: " + teacher.getPassword());
System.out.println("Teacher department: " + teacher.getDepartment());
System.out.print("\nEnter admin name: ");
String adminName = scanner.nextLine();
System.out.print("Enter admin password: ");
String adminPassword = scanner.nextLine();
System.out.print("Enter admin position: ");
String adminPosition = scanner.nextLine();
Admin admin = new Admin(adminName, adminPassword, adminPosition);
System.out.println("Admin name: " + admin.getUsername());
System.out.println("Admin password: " + admin.getPassword());
System.out.println("Admin position: " + admin.getPosition());
scanner.close();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ boolean flag = true; String requestURL = request.getRequestURI(); System.out.println(requestURL); UserInfo userInfo = (UserInfo)request.getSession() .getAttribute("user_info");
这段代码是一个拦截器的前置处理方法,用于在请求被处理之前做一些预处理工作。具体来说,它会获取当前请求的 URL,并打印出来,同时从当前会话中获取名为 "user_info" 的属性值,转换成一个 UserInfo 对象并保存在变量 userInfo 中。
如果当前会话中没有名为 "user_info" 的属性值,那么 userInfo 会被设置为 null。如果 flag 的值被设置为 false,表示请求不应该被处理(比如说,用户没有登录),否则就会继续执行后续的请求处理逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)