在这里,我想在用户关闭浏览器时注销用户。为此我做了研发,发现当我们关闭浏览器时会触发以下代码。
window.onbeforeunload = function() {
myService.logout();
return 'Your own message goes here...';
}
Run Code Online (Sandbox Code Playgroud)
在这里,当我尝试关闭浏览器时,此事件将触发,并使用户注销。但这里的问题是当页面被重定向时,这个事件也会被触发。
我想用这个功能让用户退出,但是出错了,请帮我做这个功能。
我正在使用带有 JWT 的 spring-boot。
应用程序属性
jwt.secret=xyz
Run Code Online (Sandbox Code Playgroud)
控制器.java
@PostMapping(path = "/authenticate")
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token));
}
Run Code Online (Sandbox Code Playgroud)
JwtTokenUtil.java
@Value("${jwt.secret}")
private String secret;
public String generateToken(UserDetails userDetails) {
Map<String, Object> claims = new HashMap<>();
return doGenerateToken(claims, userDetails.getUsername());
}
private String doGenerateToken(Map<String, Object> claims, String subject) {
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY*1000)).signWith(SignatureAlgorithm.HS512, secret).compact();
}
Run Code Online (Sandbox Code Playgroud)
在jwtTokenUtil.generateToken代码中我收到以下错误
ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet …Run Code Online (Sandbox Code Playgroud) 我正在使用弹簧靴。
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
Run Code Online (Sandbox Code Playgroud)
对于同一属性,我给出两个注释,一个用于空检查,另一个用于空检查。
@NotNull(message = "{email.notnull}")
@NotEmpty(message = "{email.notempty}")
private String email;
Run Code Online (Sandbox Code Playgroud)
我正在从属性文件加载错误消息。
当我将电子邮件设置为空时,我只会收到错误消息email.notempty,但是当我将电子邮件设置为空时,email.notnull并且email.notempty显示这两条错误消息,我可以知道我犯了什么错误吗?
当我给出 null 时,我应该只能看到 null 错误。
这里我使用的是Java 1.8的LocalDate类,在我的bean中,我将返回类型设为LocalDate。而且我要寄约会甲:07/01/2017。当我尝试保存时,出现了以下异常。
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-33) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method
at [Source: java.io.PushbackInputStream@39959f38; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solicitud"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method
at [Source: java.io.PushbackInputStream@39959f38; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solici"])
Run Code Online (Sandbox Code Playgroud) 我正在用Spring Security 4.0编写一个安全应用程序。作为其中的一部分,我想进行登出电话。它只是在提供不支持的请求方法“ POST”。这是我的代码:
spring-security.xml
<security:http auto-config="true">
<security:access-denied-handler error-page="/denied"/>
<security:form-login login-page="/login"
username-parameter="j_username"
password-parameter="j_password"
login-processing-url="/j_spring_security_check"
authentication-failure-url="/login?failed=true"
default-target-url="/home" always-use-default-target="true"/>
<security:custom-filter ref="secfilter" before="FILTER_SECURITY_INTERCEPTOR" />
<security:logout invalidate-session="true" logout-url="/j_spring_security_logout" logout-success-url="/login"/>
<!-- <security:logout logout-url="/j_spring_security_logout" logout-success-url="/login"/> -->
<security:csrf />
</security:http>
Run Code Online (Sandbox Code Playgroud)
jsp
<a href="j_spring_security_logout"> <button class="logoutbtn">logout</button></a>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
Run Code Online (Sandbox Code Playgroud) java ×4
spring ×2
spring-boot ×2
angularjs ×1
jackson ×1
javascript ×1
jwt ×1
jwt-auth ×1
validation ×1