小编ann*_*irq的帖子

处理 401 错误(Spring Security)

我可以处理 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)

java spring spring-security

6
推荐指数
1
解决办法
1万
查看次数

如何获得 ServletRequest 的 RequestMethod?

我有客户端机器发送的 ServletRequest。如何知道它是哪一个:GET POST UPDATEDELETE

java spring httprequest

6
推荐指数
2
解决办法
5039
查看次数

允许获取请求,但不允许后期请求(Spring Security)

我尝试配置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 **”之外的任何页面上进行身份验证

spring-security

3
推荐指数
1
解决办法
2595
查看次数

过滤器,在控制器之前获取url(Spring MVC)

我想编写过滤器,并httprequest在控制器之前获取客户端并制作一些代码,取决于URL.

请求可以是:HttpRequest,MultipartHttpServletRequest,可POST还是GET.如果此请求的URL开头,我需要向另一个REST API发出请求api.

java api rest spring dispatcher

2
推荐指数
1
解决办法
3079
查看次数

SQL查询(无重复连接)

我有桌子userstopics.每个用户可以拥有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.

sql postgresql having

1
推荐指数
1
解决办法
3240
查看次数

标签 统计

java ×3

spring ×3

spring-security ×2

api ×1

dispatcher ×1

having ×1

httprequest ×1

postgresql ×1

rest ×1

sql ×1