簡體   English   中英

當用戶未通過身份驗證時,Spring Security不會重定向到登錄頁面

[英]Spring security not redirecting to login page when users is not authenticated

我正在嘗試阻止訪問我網站中的頁面,但是下面的安全配置並不限制對私有頁面的訪問。

我的安全性配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserService detailsService;

    @Override
    protected void configure(HttpSecurity http)
        throws Exception {
        http.authorizeRequests().antMatchers("/passwordrequest/**", "/initSystem").permitAll().anyRequest()
            .authenticated().and().formLogin().loginPage("/login").usernameParameter("email").permitAll().and()
            .logout().permitAll().and().csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
        auth.userDetailsService(detailsService);
    }
}

我有一個用於/login的控制器,該控制器使用戶進入模型,以便我可以評估他們的信息。 我還根據需要實現了UserDetailsUserDetailsService

請告知可能發生的問題。 (如果要添加一些特定的代碼,請告訴我)

檢查是否在web.xml中配置了spring安全過濾器以攔截所有證券化請求。

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

您的私有頁面在配置中在哪里? 您需要具有該配置。 例:

http.authorizeRequests()
        .antMatchers("/adminpage/**").access("hasRole('ROLE_ADMIN')")
        .antMatchers("/privatepage/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')")
        .and().formLogin();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM