2017-08-14 9 views
0

私は非常に春に慣れています。Spring MVC/Security - ログイン後にログインページにアクセスすることができます

バージョン4.3.9.RELEASEのSpring MVC、Spring Securityの4.2.3.RELEASEで作業しています。彼らは正常にログインした後、ユーザーがログインページにアクセスすることができますなぜ

私が使用しています少しカスタマイズと春のログインに建てられ、ここで私のconfigureは

@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(); 

    } 
} 

のですか?私は私のような質問から学ぶことを試みましたが、彼らのどれも私と一緒に働いていません。

このソリューションは、私と一緒に動作しません:私は、Apacheのタイルを使用しています

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

、私はその部分で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> 

、ここでは

<!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> 

page.jspあるis_authenticated.jspが含まれ、レンダリングされたが、それは仕事をdoesnのさ、それは私が入れた場合にのみ動作しますあなたが見ているように間違って見えるが、別のjspファイルからインクルードされていると動作しません。

ログインコントローラからこの問題を処理する別の解決策ですが、ログインプロセスを処理するコントローラを使用していないため、私の場合は使用できません。 どうすればよいですか? カスタムログインコントローラは、Springのデフォルトのログインコントローラよりも安全ですか?

アップデート1

私は春のデフォルトのログイン機能を使用してみました:

@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(); 
} 

IはWebMvcConfigurerAdapterの私の実装でaddViewControllersを持つことになりました。 私はこのメソッドを削除しようとしましたが、login()GETのリクエストに対してのみ、ユーザーが認証された場合にリダイレクトを処理するLoginControllerリクエストを作成しようとしました。

@Controller 
public class LoginController { 

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