1
resposta

Class file has worng version 61.0 , should be 52.0 || Testando rodar aplicação depois de adicionar WebSecurityConfig

Boa tarde, quando tento compilar para testar o WebSecurityConfig estou recebendo esse erro :

C:\Users\Henrique\OneDrive\Documentos\Estudos\Spring MVC Thymeleaf e Bootstrap\mudi\src\main\java\br\com\alura\mvc\mudi\WebSecutiryConfig.java:5:67 java: cannot access org.springframework.security.config.annotation.web.builders.HttpSecurity bad class file: /C:/Users/Henrique/.m2/repository/org/springframework/security/spring-security-config/6.0.0/spring-security-config-6.0.0.jar!/org/springframework/security/config/annotation/web/builders/HttpSecurity.class class file has wrong version 61.0, should be 52.0 Please remove or make sure it appears in the correct subdirectory of the classpath.

Segue a Classe completa :

package br.com.alura.mvc.mudi;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class WebSecutiryConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
        return null;
    }
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user =
                User.withDefaultPasswordEncoder()
                        .username("joao")
                        .password("joao")
                        .roles("ADM")
                        .build();

        return new InMemoryUserDetailsManager(user);
    }
}

Alguém consegue me ajudar ?

1 resposta

Boa noite, O problema e a versão do seu JDK. Sua classe está codada em Java 17, porém você está buildando com versão 8. Qual IDE você está usando? Na IDE na estrutura do seu projeto aponte sempre para versão que estiver no POM.xml.

sobre a versão das classes: https://javaalmanac.io/bytecode/versions/