spring 无法使受保护的java.net.http.HttpRequest()可访问:模块java.net.http不会“打开java.net.http”到未命名的模块@41d477ed

gab6jxml  于 2023-11-16  发布在  Spring
关注(0)|答案(1)|浏览(196)

以下是我的错误:

2023-11-07T23:21:13.207+07:00 ERROR 22720 --- [nio-8000-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.reflect.InaccessibleObjectException: Unable to make protected java.net.http.HttpRequest() accessible: module java.net.http does not "opens java.net.http" to unnamed module @41d477ed] with root cause

java.lang.reflect.InaccessibleObjectException: Unable to make protected java.net.http.HttpRequest() accessible: module java.net.http does not "opens java.net.http" to unnamed module @41d477ed
    at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:387) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:363) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:311) ~[na:na]

字符串
我已经试过检查java的安全性,但它不工作,并有上述错误

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        http
                .csrf(AbstractHttpConfigurer::disable)
//                .securityContext((securityContext) -> securityContext.requireExplicitSave(true))
                .authorizeHttpRequests(req ->  req.requestMatchers("/api/v1/auth/**")
                        .permitAll()
                        .anyRequest()
                        .authenticated()

                )
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authenticationProvider(authenticationProvider)
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }


下面是我的HttpRequest过滤器和我的请求:http://localhost:8000/api/v1/auth/signup我认为在配置authorizeHttpRequests(req -> req.requestMatchers(“/api/v1/auth/**”)时出错

@Override
    protected void doFilterInternal(  HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if(request.getServletPath().contains("/api/v1/auth")){
            filterChain.doFilter(request,response);
            return;
        }
        final String authHeader = request.getHeader("Authorization");
        final String jwt;
        final String userEmail;
        if (authHeader==null || !authHeader.startsWith("Bearer ")){
            filterChain.doFilter(request,response);
        }
        assert authHeader != null;
        jwt = authHeader.substring(7);
        userEmail = jwtService.extractUsername(jwt);
        //        SecurityContextHolder là nơi SpringSecurity lưu trữ thông tin xác thực.
        //SecuritySecurity sẽ không xác thực cách tạo SecurityContextHolder.
        //Khi nó tìm thấy các giá trị trong SecurityContextHolder, nó giả định rằng người dùng hiện tại là người dùng được xác thực.
        if(userEmail!=null && SecurityContextHolder.getContext().getAuthentication()==null){
            UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
            var isTokenValid = tokenRepo.findTokenByToken(jwt).map(token -> !token.isExpired() && token.revoked).orElse(false);
            if(jwtService.isTokenValid(jwt,userDetails)&& isTokenValid){
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
                        userDetails,
                        null,
                        userDetails.getAuthorities()
                );
//                authenticationToken.setDetails(
//                        new We
//                );
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }
        filterChain.doFilter(request,response);
    }

pokxtpni

pokxtpni1#

该错误似乎表明,在您的应用程序中的某个地方,它试图使用类java.net.http.HttpRequest的构造函数。由于该构造函数被声明为protected(protected HttpRequest()),因此您不能直接使用。相反,您必须使用其中一个构建器方法来构造HttpRequest示例。例如HttpRequest.newBuilder()。有关示例,请参阅https://www.baeldung.com/java-9-http-client

相关问题