简体   繁体   English

Spring Security自定义登录页面不起作用

[英]Spring Security Custom login page is not working

I am making enterprise webapp, i have built my custom login page but somehow only spring security login page is coming instead of my custom login page. 我正在制作企业级Web应用程序,已经构建了自定义登录页面,但是不知何故,只有Spring Security登录页面出现了,而不是我的自定义登录页面。 Below is my security configuration class. 下面是我的安全配置类。

please help. 请帮忙。

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication().withUser("test").password("pwd123")
            .roles("USER", "ADMIN");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/login").permitAll()
            .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and()
            .formLogin();
        http.csrf().disable();
}

You have to specify the url to your custom login page. 您必须指定自定义登录页面的URL。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/login").permitAll()
        .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and()
        .formLogin()

         // put the relative URL to your custom login here
             .loginPage("/login")
             .permitAll();
    http.csrf().disable();
}

Hope this helps. 希望这可以帮助。

Look every thing good, this is work for me: 看起来一切都很好,这对我来说很有效:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
         .authorizeRequests()
             .antMatchers("/todo/**").hasRole("USER")
             .antMatchers("/", "/home").permitAll()
             .antMatchers("/login").permitAll()
             .anyRequest().authenticated()
             .and()
        .formLogin()
             .loginPage("/login")
             .defaultSuccessUrl("/home")
             .permitAll()
             .and()
         .logout() 
             .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
             .logoutSuccessUrl("/home")
             .permitAll()
             .and()
         .exceptionHandling().accessDeniedPage("/403");
  }

Here is a Spring Boot project. 这是一个Spring Boot项目。

@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); @Override protected void configure(HttpSecurity http) throws Exception { http.csrf()。disable(); http.authorizeRequests().antMatchers("/login").permitAll() .anyRequest().authenticated().and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .failureUrl("/login?error=true") .permitAll() .and() .logout() .logoutSuccessUrl("/login?logout=true") .invalidateHttpSession(true) .deleteCookies("JSESSIONID") .permitAll(); }

Reference : https://devkonline.com/tutorials/content/SPRING-SECURITY-5-CUSTOM-FORM-LOGIN-THYMELEAF 参考: https : //devkonline.com/tutorials/content/SPRING-SECURITY-5-CUSTOM-FORM-LOGIN-THYMELEAF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM