Как отключить стандартную страницу безопасности / входа в систему? - PullRequest
0 голосов
/ 11 июня 2019

Я не могу заставить Spring-Security отображать мою собственную страницу входа.Я перепробовал почти все доступные решения.Я не знаю, что не так с моим кодом.

Ниже приведен код и закомментированная часть кода, которую я уже пробовал.

Версия SpringBoot - 2.1.5

SecurityConfig.class

@EnableWebSecurity
@ComponentScan
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
public UserDetailsService userDetailsService(){
    return super.userDetailsService();
}

@Override
public void configure(HttpSecurity httpSecurity) throws Exception{

    httpSecurity.
            authorizeRequests()
            .antMatchers("/resources/**","/login", "/home").permitAll()
            .antMatchers("/user/**").hasRole("USER")


            .and()
            .formLogin().loginPage("/login").permitAll()
                .defaultSuccessUrl("/user/dashboard")
                .failureUrl("/login-error")

            .and().logout().permitAll();

//////////// Tried this too /////////////////////////////////////
//        httpSecurity.cors().disable()
//                .csrf().disable()
//                .httpBasic().disable()
//                .authorizeRequests().antMatchers("/**").permitAll();

}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
    authenticationManagerBuilder.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}

}

Просмотр класса контроллера:

@Controller
@RequestMapping("/")
public class ViewController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping(value = {"/home"})
    public String showHome(){ return "home.html";}

    @GetMapping(value = "/login")
    public String showLogin(){
    return "login.html";
}

Я хочу, чтобы spring-security отключила собственную страницу входа по умолчанию и отобразила мою страницу входа.

Ответы [ 2 ]

0 голосов
/ 13 июня 2019

Что ж, странно, но я только что создал новый проект, скопировал старые файлы проекта в новый и он работает, как и ожидалось.

0 голосов
/ 11 июня 2019

Вам необходимо настроить WebMvcConfigurer

добавьте приведенный ниже класс в качестве вашей конфигурации и переопределите метод addViewControllers

@EnableWebMvc
@Configuration
public class WebConfiguration implements WebMvcConfigurer {


        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {

            registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
        }

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/login").setViewName("login");
        }
    }

после этого разрешите доступ к вашим ресурсам в настройках безопасности

http.authorizeRequests().antMatchers("/resources/**", "/css/**", "/js/**", "/img/**").permitAll()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...