2017-08-14 82 views
0

我對Spring很陌生。Spring MVC /安全性 - 成功登錄後用戶仍然可以訪問登錄頁面

我正在使用Spring MVC的4.3.9.RELEASE,4.2.3.RELEASE的Spring Security。

我用內置彈簧的登錄與少量的自定義,這是我的配置

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService myUserService; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {  
     auth 
      .userDetailsService(myUserService); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
      .rememberMe() 
       .alwaysRemember(true) 
       .and() 
      .logout() 
       .permitAll(); 

    } 
} 

他們爲什麼成功登錄後,用戶可以訪問登陸頁面?我試圖從與我的相同的問題中學習,但沒有一個與我一起工作。

此解決方案不會和我一起工作:

<sec:authorize access="isAuthenticated()"> 
    <% response.sendRedirect(request.getContextPath()); %> 
</sec:authorize> 

我使用Apache瓷磚,我有is_authenticated.jsp的那部分。這是tiles.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE tiles-definitions PUBLIC 
     "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" 
     "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 

<tiles-definitions> 
    <definition name="base" template="/WEB-INF/layouts/page.jsp"> 
    <put-attribute name="pageHeader" value="/WEB-INF/layouts/page_header.jsp"></put-attribute> 
    <put-attribute name="pageFooter" value="/WEB-INF/layouts/page_footer.jsp"></put-attribute> 
    </definition> 

    <definition name="login" extends="base"> 
    <put-attribute name="isAuthenticated" value="/WEB-INF/views/is_authenticated.jsp"></put-attribute> 
    <put-attribute name="pageBody" value="/WEB-INF/views/login.jsp"></put-attribute> 
    </definition> 

    <definition name="home" extends="base"> 
    <put-attribute name="isAuthenticated" value=""></put-attribute> 
    <put-attribute name="pageBody" value="/WEB-INF/views/home.jsp"></put-attribute> 
    ... 
</tiles-definitions> 

,這裏是page.jsp

<!DOCTYPE> 
<html> 
<head> 
    <t:insertAttribute name="isAuthenticated"></t:insertAttribute> 
    ... 
</head> 

<body> 
    <!-- Page Layout HTML --> 
    <header id="pageHeader"> 
    <t:insertAttribute name="pageHeader"></t:insertAttribute> 
    </header> 

    <main id="pageBody"> 
    <t:insertAttribute name="pageBody"></t:insertAttribute> 
    </main> 
    ... 
</body> 
</html> 

的is_authenticated.jsp包括和渲染,但沒有關係的工作,但它如果我把唯一的工作在page.jsp本身內部阻塞,看起來是錯誤的,但是當從另一個jsp文件包含時它不起作用。

另一個解決方案,從登錄控制器處理這個問題,但這在我的情況下是不可用的,因爲我沒有使用任何控制器來處理登錄過程。 我應該怎麼做? 自定義登錄控制器將比Spring中的默認登錄控制器更安全嗎?

UPDATE1

我試圖用春天的默認登錄功能:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService myUserService; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {  
     auth 
      .userDetailsService(myUserService); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin(); 

    } 
} 

但是我發現,成功登錄後,用戶仍然可以訪問登錄頁面。 所以我猜我需要在LoginController中有一個方法來完成這個。

+0

爲什麼您需要阻止訪問經過身份驗證的用戶的登錄頁面? – StanislavL

+0

@StanislavL 上午,我雖然認爲登錄頁面不應該被訪問,如果用戶已經通過身份驗證,就像我以前使用在線Web應用程序的體驗一樣。 –

回答

0

我想我找到了答案。

由於我配置configure方法是:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .antMatchers("/resources/**").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login") 
      .permitAll() 
      .and() 
     .rememberMe() 
      .alwaysRemember(true) 
      .and() 
     .logout() 
      .permitAll(); 
} 

我應該有addViewControllers我在執行的WebMvcConfigurerAdapter。 因此,我試圖刪除此方法,而是創建一個LoginController,它只有login()方法的GET請求處理重定向,如果用戶通過身份驗證。

@Controller 
public class LoginController { 

    @RequestMapping(value="/login", method=GET) 
    public String login(Principal principal) { 
     return principal == null ? "login" : "redirect:/"; 
    } 
} 
相關問題