我可以处理 404 错误。
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public void noHandlerFoundException (HttpServletResponse response) throws IOException{
//some code
}
Run Code Online (Sandbox Code Playgroud)
但是如何处理 401 错误呢?
编辑我使用的是 Java 而不是 web.xml
编辑我应该在 NoHandlerFoundException 中放入什么来处理 HttpStatus.UNAUTHORIZED
编辑
当身份验证失败时,我有方法 unsuccessfulAuthentication:
public class StatelessLoginFilter extends AbstractAuthenticationProcessingFilter {
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
SecurityContextHolder.clearContext();
if (logger.isDebugEnabled()) {
logger.debug("Authentication request failed: " + failed.toString());
logger.debug("Updated SecurityContextHolder to contain null Authentication");
logger.debug("Delegating to authentication failure handler " + failureHandler);
}
// response.setCharacterEncoding("UTF-8");
// …Run Code Online (Sandbox Code Playgroud) 我有客户端机器发送的 ServletRequest。如何知道它是哪一个:GET POST UPDATE或DELETE?
我尝试配置Spring Security。我有以下配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/auth**").permitAll()
.and()
.authorizeRequests()
.antMatchers("/**").authenticated()
.anyRequest().permitAll()
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.userDetailsService(userDetailsService());
}
Run Code Online (Sandbox Code Playgroud)
我可以发送get-request链接以“ / auth”开头,但是如果没有身份验证就无法发送后请求。如果删除跟随代码,一切正常:
.authorizeRequests()
.antMatchers("/**").authenticated()
.anyRequest().permitAll()
Run Code Online (Sandbox Code Playgroud)
但是我需要在除“ / auth **”之外的任何页面上进行身份验证
我想编写过滤器,并httprequest在控制器之前获取客户端并制作一些代码,取决于URL.
请求可以是:HttpRequest,MultipartHttpServletRequest,可POST还是GET.如果此请求的URL开头,我需要向另一个REST API发出请求api.
我有桌子users和topics.每个用户可以拥有0到几个主题(一对多关系).
我如何才能获得至少有一个主题的用户?
我需要来自用户的所有列(没有主题中的列),并且表中没有重复项users.在上一篇专栏文章中,我需要多少主题.
更新:
应该是这样的:
SELECT user.*, count(topic.id)
FROM ad
LEFT JOIN topic ON user.id = topic.ad
GROUP BY user.id
HAVING count(topic.id) > 0;
Run Code Online (Sandbox Code Playgroud)
但它需要0结果.但它不应该是0.