gpt4 book ai didi

java - 如何仅通过链接设置 jsp 页面的访问权限

转载 作者:行者123 更新时间:2023-11-30 06:26:13 25 4
gpt4 key购买 nike

我是 Spring 的新手,但在这里我遇到了一些问题,我找不到任何答案。因此,我需要为注册页面设置访问权限,仅适用于拥有管理员链接的用户。我看到,管理员发送链接,只有一个用户可以进入并注册,之后该链接将不可用。首先,我知道管理员应该生成一些链接,但我不知道如何生成。我不知道如何通过链接设置该访问权限。好吧,我可以说我什么都不知道:)你能帮助我吗?就像我什至找不到有关此的一些教程或信息。代码是:

protected void configure(final HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/index").hasAnyRole(USER, ADMIN)
.antMatchers("/admin").hasRole(ADMIN)
.antMatchers("/addUser").hasRole(ADMIN)
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/index")
.failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling()
.accessDeniedPage("/login")
.and()
.rememberMe()
.rememberMeParameter("remember-me")
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(900);
}

管理页面,其中有注册新用户的按钮

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Users List</title>
<link href="<c:url value='/resources/css/bootstrap.min.css' />" rel="stylesheet"></link>

</head>

<body>
<div class="generic-container">
<%--<%@include file="authheader.jsp" %>--%>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading"><span class="lead">User Administration</span></div>
<table class="table table-hover">
<thead>
<tr>
<th>Username</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Role</th>
<sec:authorize access="hasRole('ADMIN')">
<th width="100"></th>
</sec:authorize>
<sec:authorize access="hasRole('ADMIN')">
<th width="100"></th>
</sec:authorize>
</tr>
</thead>

<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.username}</td>
<td>${user.firstname}</td>
<td>${user.lastname}</td>
<td>${user.email}</td>
<td>${user.roles}</td>
<sec:authorize access="hasRole('ROLE_ADMIN')">
<td><a href="<c:url value='/edit-user-${user.username}' />" class="btn btn-success custom-width">edit</a></td>
</sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">
<td><a href="<c:url value='/delete-user-${user.username}' />" class="btn btn-danger custom-width">delete</a></td>
</sec:authorize>
</tr>
</c:forEach>
</tbody>
</table>
</div>

<sec:authorize access="hasRole('ADMIN')">
<div class="well">
<a href="<c:url value='/addUser' />">Add New User</a>
</div>
</sec:authorize>
</div>
</body>
</html>

如果您需要一些额外的代码,请告诉我。抱歉,如果有问题,这是我的第二个问题。

最佳答案

您可以生成一个 token (很难猜测)

UUID id = UUID.randomUUID();
String t = id.toString();
//your link generation goes here
String link = baseUrl+"/register?token="+t;
//Save the token in db with a field expired=false
Token token = new Token();
token.setUuid(t);
token.setCreatedDate(new Date());
token.setExpired(false);
tokenRepo.save(token)

在请求处理中,将 token 获取到 RestController 中并

//let t be the captured token
Token token = tokenRepo.findByUuid(t);
if(token.expired){
//tell them link is expired
}else{
token.setExpired(true)
//give them the reg page and take it from here
}

关于java - 如何仅通过链接设置 jsp 页面的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com