0

jwtログインの例を実装しようとしています

セキュリティクラスのconfigureメソッドを構成しているときに、いくつかの問題に直面しています

ここにコントローラーのメソッドがあります

@PostMapping("api/user/login")
public ResponseEntity<?> login(Principal principal)
{
    if (principal == null)
    {
        return ResponseEntity.ok(principal);
    }
    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) principal;
    User user = userService.findByUsername(token.getName());
    user.setToken(jwtTokenProvider.generateToken(token));
    return new ResponseEntity<>(user, HttpStatus.OK);
}

ここにhttp構成バージョンがあります

@Override
protected void configure(HttpSecurity http)
    throws Exception
{
    http.cors().and().authorizeRequests().antMatchers("/resources/**", "/error", "/api/user/**")
        .permitAll().antMatchers("/api/admin/**").hasRole("ADMIN").anyRequest()
        .fullyAuthenticated().and().logout().permitAll()
        .logoutRequestMatcher(new AntPathRequestMatcher("/api/user/logout", "POST")).and()
        .formLogin().loginPage("/api/user/login/").permitAll().and().httpBasic().and().csrf()
        .disable();
    http.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtTokenProvider));
}

明らかに、私はログインAPIをpermitallとして許可しましたが、それでもフィルターを使用しています.誰が正確に何が起こっているのか説明できますか

フィルタ コード

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                FilterChain chain)
    throws IOException,
    ServletException
{
    Authentication authentication = jwtTokenProvider.getAuthentication(request);
    if (authentication != null && jwtTokenProvider.validateToken(request))
    {
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }
    chain.doFilter(request, response);
}

私の学習によると、http構成でpermitallとして許可している場合、フィルターコードに入るべきではありません。間違っている場合は修正してください。

4

0 に答える 0