authorizeRequest()弃用后Spring Boot Security自定义登录页失效问题
自定义Spring Boot Security登录页遇「需要授权」问题
我尝试用自定义Thymeleaf登录页替换Spring Boot Security的默认登录页,但未达到预期效果,反而弹出「需要授权」的提示。以下是弃用authorizeRequest()和antMatchers()后的相关代码:
安全配置代码
@EnableWebSecurity @Configuration public class HospitalSecurityConfig { @Bean public SecurityFilterChain filterChain (HttpSecurity http) throws Exception { http .authorizeHttpRequests() .requestMatchers("/").authenticated() .requestMatchers("/mylogin").permitAll() .and() .formLogin().loginPage("/mylogin").loginProcessingUrl("/mylogin").permitAll() .and() .logout().logoutUrl("/logout") .and() .authenticationProvider(authProvider()) .httpBasic() ; return http.build(); } @Bean public UserDetailsService detailsService() { return new HospitalUserDetailsServiceer(); } @Bean public AuthenticationProvider authProvider() { DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); provider.setPasswordEncoder(encoder()); provider.setUserDetailsService(detailsService()); return provider; } @Bean public PasswordEncoder encoder() { return new BCryptPasswordEncoder(); } }
myLogin.html代码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <meta charset="ISO-8859-1"> <title>Welcome to HealthMe Application!</title> <link th:href="@{/css/cssLogin.css}" rel="stylesheet" /> <script type="text/javascript" th:src="@{/js/jsLogin.js}"></script> </head> <body> <form th:action="@{/mylogin}" method="post"> Username = <input name="username" type="text"> <br> Password = <input name="password" type="password"> <br> <input type="submit"> </form> </body> </html>
MVC配置代码
@EnableWebMvc @Configuration public class HospitalMVCconfig implements WebMvcConfigurer{ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/mylogin").setViewName("myLogin"); } }
内容的提问来源于stack exchange,提问作者Saman KHATAEI
相关产品推荐
相关产品推荐

