我在这里处理JWT并设置自定义字段。 所有这些自定义字段均在枚举中描述:
public enum JwtFields {
userId,
manyOtherCustomFieldsBellow,
blaBlaBla
}
因此,每当我创建令牌而不是为密钥传递字符串时,我都会使用枚举,因为它更快,更安全。
Claims claims = Jwts.claims().setSubject(userId);
claims.put(JwtFields.someCustomFieldFromEnum.name(), "someValue")
现在,一旦我检查接收到的令牌是否有效并且是否存在所有必要的自定义字段,就想将其反序列化到某些TokenDecoded
类并将其附加到请求有效负载,因此无论何时我处理一个请求,我将拥有JWT令牌中的所有值和字段。
但是,该反序列化类几乎包含了枚举中的所有字段,如果明天我将向枚举中添加新字段,则我还必须手动更新我的TokenDecoded
类以包括该新的自定义字段
问题:
如何使该TokenDecoded
类基于枚举字段,所以如果我向枚举添加新字段,它将自动出现在TokenDecoded
中?是否有反思?还是可以更简单地实现?
答案 0 :(得分:1)
Lombok提供了一种以另一种方式运行的功能:如果您在类中定义字段,则可以用body {
font-family: 'Roboto', sans-serif;
padding: 0;
margin: 0;
}
.ui-autocomplete li {
border: 1px solid #003eff;
background: black;
color: red;
font-weight: normal;
}
#container nav ul {
color: navy;
position: absolute;
left: 50%;
top: 5%;
transform: translate(-50%, -5%);
list-style-type: none;
font-size: 15px;
margin: 0;
padding: 0;
text-align: center;
width: 100%;
font-weight: bold;
}
#container nav li {
display: inline-block;
width: 31%;
padding: 1%;
}
#main {
color: midnightblue;
text-align: center;
position: absolute;
left: 50%;
top: 40%;
transform: translate(-50%, -40%);
}
#main h3 {
line-height: 1.5em;
}
img {
width: 100vw;
height: 100vh;
}
form {
text-align: center;
position: absolute;
left: 50%;
top: 20%;
transform: translate(-50%, -20%);
font-size: 40px;
}
form label {
margin-bottom: 7px;
}
form input[type="text"] {
padding: 10px;
border: none;
display: block;
width: 600px;
border-bottom: 2px solid black;
text-align: center;
}
input:focus {
outline: none;
}
button {
color: white;
font-weight: bold;
display: block;
position: absolute;
left: 50%;
top: 65%;
transform: translate(-50%, -65%);
width: 250px;
padding: 12px;
border: none;
background: linear-gradient(70deg, rgba(175, 238, 238, 0.6) 50%, rgba(135, 206, 250, 0.6) 50%);
letter-spacing: 0.15em;
}
button:hover {
animation: change 0.2s linear forwards;
}
@keyframes change {
from {
color: white;
font-size: 20px;
background: linear-gradient(40deg, rgba(175, 238, 238, 0.1) 50%, rgba(135, 206, 250, 0.1) 50%);
}
to {
color: royalblue;
font-size: 25px;
background: linear-gradient(40deg, rgba(175, 238, 238, 0) 50%, rgba(135, 206, 250, 0) 50%);
}
}
对其进行注释,以基于字段名称生成枚举。或者,如果没有<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="vezovi.css" type="text/css" rel="stylesheet">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="icon" type="image/png" sizes="32x32" href="assets/img/icons/favicon-32x32.png">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$('button').click(function() {
$('#container, img').fadeOut(500),
$('form').delay(1000).fadeIn(500);
});
let gradovi = ['Rovinj', 'Mooo', 'Meeeaw?', 'mowmow?'];
$('#lokacija').autocomplete({
source: gradovi
});
$('gradovi').css('color', 'red');
});
</script>
</head>
<body>
<img src="https://komarna-croatia.com/wp-content/uploads/2016/04/Sea-Wallpaper-Travel.jpg">
<div id="container">
<nav>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
</nav>
<div id="main">
<h1>Cities</h1>
<h3>Click on the button</h3>
</div>
<button style="font-size: 20px; border-radius: 25px;">Klikni ovdje</button>
</div>
<form style="display: none;" method="POST" target="_self" action="lokacija.html">
<label for="lokacija">Enter location</label>
<input type="text" id="lokacija" value="" name="location">
</form>
<!-- <script src="vezovi.js"></script> -->
</body>
</html>
参数,则只需要将其作为字符串使用即可获得@FieldNameConstants(asEnum = true)
字段。
答案 1 :(得分:0)
您有几种选择:
我认为在编译时生成代码最适合您的情况。
请注意,如果您使用的是JWT,则可能需要研究a JWT library而不是重新发明轮子。