我无法将我的 web 应用程序更新到 Spring Boot 2.6.0(2.5.7 可以工作,但 2.6.0 不行)

Fed*_*rti 10 spring-mvc spring-security spring-boot

正如标题中提到的,我无法将我的 web 应用程序更新到 Spring Boot 2.6.0。我使用 Spring Boot 2.5.5 编写了我的 web 应用程序,一切都运行良好。如果我使用这个新标签更新 pom.xml 文件:

\n
<version>2.5.7</version>\n
Run Code Online (Sandbox Code Playgroud)\n

我的网络应用程序运行完美。所有测试均有效。\n如果我执行此更新,则 Web 应用程序不会启动:

\n
<version>2.6.0</version>\n
Run Code Online (Sandbox Code Playgroud)\n

启动调试模式,IDE 向我显示一个错误和 2 个指向我的 web 应用程序的 2 个类的链接。

\n
2021-11-23 00:31:45.419 ERROR 21884 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed\n\norg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'configurazioneSpringSecurity': Requested bean is currently in creation: Is there an unresolvable circular reference?\n
Run Code Online (Sandbox Code Playgroud)\n

看来问题出在这个类中:

\n
@Configuration\n@EnableWebSecurity\npublic class ConfigurazioneSpringSecurity extends WebSecurityConfigurerAdapter {\n\n    @Autowired\n    LivelliDeiRuoli livelliDeiRuoli;\n\n    @Autowired\n    GestioneUtentiSpringSecurity gestioneUtentiSpringSecurity;\n\n    @Bean\n    public BCryptPasswordEncoder metodoCrittografia() {\n        return new BCryptPasswordEncoder();\n    }\n\n    @Autowired\n    public void crittografiaPassword(AuthenticationManagerBuilder auth) throws Exception {\n        auth.userDetailsService(gestioneUtentiSpringSecurity).passwordEncoder(metodoCrittografia());\n    }\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n\n        http.csrf().disable();\n\n        http.authorizeRequests().antMatchers(\n                "/",\n                "/login",\n                "/benvenuto",\n                "/registrazione",\n                "/registrazione-eseguita",\n                "/pagine-applicazione"\n        ).permitAll();\n\n        http.authorizeRequests().antMatchers("/area-riservata")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(1L) + "')");\n\n        http.authorizeRequests().antMatchers("/cambio-password")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(1L) + "')");\n\n        http.authorizeRequests().antMatchers("/cambio-nome")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(1L) + "')");\n\n        http.authorizeRequests().antMatchers("/cancella-utente")\n                .access("isAuthenticated()");\n\n        http.authorizeRequests().antMatchers("/gestione-utenti")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(2L) + "')");\n\n        http.authorizeRequests().antMatchers("/gestione-ruoli")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(3L) + "')");\n\n        http.authorizeRequests().antMatchers("/pannello-di-controllo")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(3L) + "')");\n\n        http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/errore-403");\n\n        http.authorizeRequests().and().formLogin()\n                .loginProcessingUrl("/pagina-login")\n                .loginPage("/login")\n                .defaultSuccessUrl("/")\n                .failureUrl("/login?errore=true")\n                .usernameParameter("username")\n                .passwordParameter("password")\n                .and().logout().logoutUrl("/pagina-logout")\n                .logoutSuccessUrl("/login?logout=true");\n\n        http.authorizeRequests().and() //\n                .rememberMe().tokenRepository(this.persistentTokenRepository()) //\n                .tokenValiditySeconds(365 * 24 * 60 * 60);\n                \n        http.authorizeRequests().antMatchers("/gestione-eventi")\n                .access("hasAnyRole('" + livelliDeiRuoli.elencoRuoli(2L) + "')");\n\n        http.authorizeRequests().antMatchers(\n                "/cerca-eventi",\n                "/ultimi-eventi"\n        ).permitAll();\n\n    }\n\n    @Autowired\n    private DataSource dataSource;\n\n    @Bean\n    public PersistentTokenRepository persistentTokenRepository() {\n        JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl();\n        db.setDataSource(dataSource);\n        return db;\n    }\n\n    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)\n    @Override\n    public AuthenticationManager authenticationManagerBean() throws Exception {\n        return super.authenticationManagerBean();\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

或者在这个:

\n
@SpringBootApplication\n@Profile("sviluppo")\npublic class GestioneUtentiApplication extends SpringBootServletInitializer {\n\n    @Override\n    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {\n        return application.sources(GestioneUtentiApplication.class);\n    }\n\n    public static void main(String[] args) {\n        System.setProperty("server.servlet.context-path", "/gestioneutenti");\n        SpringApplication.run(GestioneUtentiApplication.class, args);\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这些课程有什么问题吗?

\n

Spring Boot 2.6.0 有哪些变化?

\n

GestioneUtentiSpringSecurity 实现 UserDetailsS​​ervice:

\n
@Service\npublic class GestioneUtentiSpringSecurity implements UserDetailsService {\n\n    @Autowired\n    private UtenteRepository utenteRepository;\n\n    @Autowired\n    private RuoloRepository ruoloRepository;\n\n    @Autowired\n    EseguiVariabiliDiSistema eseguiVariabiliDiSistema;\n    \n    @Autowired\n    LivelliDeiRuoli livelliDeiRuoli;\n\n    @Override\n    public UserDetails loadUserByUsername(String nomeUtente) throws UsernameNotFoundException {\n\n        Utente utente = trovaUtenteConPrivilegiDiAutenticazione(nomeUtente);\n\n        if (utente == null) {\n            throw new UsernameNotFoundException("L'utente " + nomeUtente + " non \xc3\xa8 stato trovato nel database.");\n        }\n\n        List<String> ruoliUtente = null;\n        try {\n            ruoliUtente = this.ruoloRepository.trovaRuoliUtente(utente.getId());\n        }catch (Exception b){\n            ruoliUtente = null;\n        }\n\n        List<GrantedAuthority> grantList = null;\n        try{\n            grantList = new ArrayList<GrantedAuthority>();\n            if (ruoliUtente != null) {\n                for (String ruolo : ruoliUtente) {\n                    GrantedAuthority authority = new SimpleGrantedAuthority(ruolo);\n                    grantList.add(authority);\n                }\n            }\n        }catch (Exception c){\n            grantList = null;\n        }\n\n        UserDetails userDetails = null;\n        if((utente != null) && (ruoliUtente != null) && (grantList != null)){\n            userDetails = (UserDetails) new User(utente.getNome(), utente.getPassword(), grantList);\n        }\n        return userDetails;\n    }\n\n    public Utente trovaUtenteConPrivilegiDiAutenticazione(String nomeUtente){\n        try{\n            Utente utente = utenteRepository.trovaUtente(nomeUtente);\n            if(livelliDeiRuoli.requisitiUtenteConRuoloMassimo(utente)){\n                return utente;\n            } else{\n                eseguiVariabiliDiSistema.trovaVariabileSenzaVerificaUtente(\n                        new VariabileSistema(0L, "login", "")\n                );\n                if(eseguiVariabiliDiSistema.getVariabileDiSistema().getValore().equals("true")){\n                    return utente;\n                }else if(eseguiVariabiliDiSistema.getVariabileDiSistema().getValore().equals("false")){\n                    return null;\n                }else{\n                    return null;\n                }\n            }\n        }catch (Exception e){\n            return null;\n        }\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Joã*_*ias 13

从 Spring Boot 2.6 开始,默认禁止循环依赖。您可以通过设置以下属性再次允许循环引用:

spring.main.allow-circular-references = true
Run Code Online (Sandbox Code Playgroud)

您可以在Spring Boot 2.6 发行说明中阅读有关此内容的更多详细信息。