2017-04-27 58 views
0

我在春季啓動應用程序上使用spring安全性,我無法使用我製作的自定義登錄頁面。我想在默認情況下使用自定義登錄頁面,而不使用配置控制器,而是想要從application.properties更改配置。此外,我想拒絕訪問其他網頁,直到登錄成功。請幫我註銷應用程序。如何在沒有配置的情況下爲自定義登錄頁面使用spring boot security?

+0

我沒有配置,我問,我想使用自定義登錄頁面,而不配置或使用application.properties文件進行配置。 –

回答

1

Spring Security Reference提供了關於如何實現自定義登錄頁面的extensive guide

我組建了一個小例子中this repo

+0

我們是否需要使用WebSecurityConfigurerAdapter類。我們不能使用application.properties而不是WebSecurityConfigurerAdapter類嗎? –

+0

沒有辦法單獨使用我知道的'application.properties' **來實現**,我懷疑是否存在。 我建議的方法是春季隊推薦的方法。 –

0

Spring Security With Customn login Page

已與sitemash + Hibernate一同也實現提供了自定義登錄頁面的Spring Security的一個例子,你必須創建啞巴給出數據庫在自述文件中。

3

定義自定義的login.jsp

<form method="POST" action="${contextPath}/login" class="form-signin"> 
     <h2 class="form-heading">Log in</h2> 

     <div class="form-group ${error != null ? 'has-error' : ''}"> 
      <span>${msg}</span> 
      <input name="username" type="text" class="form-control" placeholder="Username" 
        autofocus="true"/> 
      <input name="password" type="password" class="form-control" placeholder="Password"/> 
      <span>${errorMsg}</span> 

      <button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button> 
     </div> 

    </form> 

接下來的控制器,添加它會返回/登錄RequestMapping

@RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String login(Model model, String error, String logout) { 
     if (error != null) 
      model.addAttribute("errorMsg", "Your username and password are invalid."); 

     if (logout != null) 
      model.addAttribute("msg", "You have been logged out successfully."); 

     return "login"; 
} 

最後修改春自定義登錄頁面GET方法安全配置指定上述請求映射

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/welcome").hasAnyRole("USER", "ADMIN") 
       .antMatchers("/getEmployees").hasAnyRole("USER", "ADMIN").antMatchers("/addNewEmployee") 
       .hasAnyRole("ADMIN").anyRequest().authenticated() 
       .and().formLogin().loginPage("/login").permitAll() 
       .and().logout().permitAll(); 

    } 

詳細的說明和工作的源代碼Spring Boot Security - Creating a custom Login Page

相關問題