2017-05-18 209 views
0

我是Spring Security的新手,我試圖用Spring Security創建一個登錄表單。Spring Security - 配置HttpSecurity

這是必要的方案:

1)用戶登錄與用戶名的應用程序 - 密碼(請注意,我用Spring Security中提供的默認loginpage) 2)如果登錄OK,用戶去eventList.jsp 3)如果登錄KO(錯誤的憑據)錯誤顯示

我WebSecurityConfigurerAdapter配置:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.inMemoryAuthentication().withUser("amdin").password("111111").roles("USER"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http 
      .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
      .formLogin().defaultSuccessUrl("/eventList"); 
} 

錯誤1:如果我插入正確的CRE dentials我沒有看到/ eventList,但我收到一個404(/spring-security-helloworld-annotation/WEB-INF/pages/login.jsp)。爲什麼我不重定向到/ eventList? (pheraps因爲/ EVENTLIST僅接受我RequestMapping註釋GET

@RequestMapping(value = {"/eventList"}, method = RequestMethod.GET) 

錯誤2:如果我嘗試「手動」轉到/ EVENTLIST,加入「EVENTLIST」到URL在瀏覽器中結束,我可以在不執行登錄操作的情況下訪問請求的頁面!!!我唯一可以訪問而不執行登錄操作的URL是登錄頁面本身!!!

該行.anyRequest().authenticated()不應該允許所有這些! !!

我怎麼能得到我想要的?

TY提前

+1

您是否使用Spring Boot?對於第一個錯誤,你有定義的/ eventList控制器嗎?對於錯誤2,確實很奇怪。在您的控制檯日誌中,您是否有DefaultSecurityFilterChain條目? –

+0

1)我有一個/ eventList控制器(事實上,如果我試圖通過在瀏覽器中輸入URI來訪問它,我可以訪問該頁面) –

回答

0

正確的安全鏈看起來如下:

  http 
       .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .defaultSuccessUrl("/eventList") 
       .permitAll() 
       .and() 
       .logout() 
       .permitAll(); 

你忘了permitAll()聲明以及定義loginPage() 希望它能幫助!如果你需要進一步的幫助,請給我一個下午。