2015-12-02 99 views
0

我目前在angularJS路由和Spring Security默認登錄表單之間存在衝突問題。如果我把一個角度路由作爲登錄頁面,它不喜歡這樣,似乎開始一個無限的重定向循環。Spring Security和AngularJS路由之間的衝突

的Index.html

<html ng-app='storeApp'> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Online Shopping System</title> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
</head> 
<body> 

    <div class="container" ng-controller="testCtrl as tVm"> 
     {{authenticated}} 
     <ul class="nav nav-pills" role="tablist"> 
      <li class="active"><a href="#/">Home</a></li> 
      <li><a href="#/customer">Login</a></li> 
      <li ng-show="authenticated"><a href="" ng-click="tVm.logout()">Logout</a></li> 
     </ul> 
    </div> 
    <div class="container-fluid" ng-view></div> 

</body> 

<script src="https://code.angularjs.org/1.4.7/angular.min.js"></script> 
<script src="https://code.angularjs.org/1.4.7/angular-route.min.js"></script> 
<script src="https://code.angularjs.org/1.4.7/angular-resource.min.js"></script> 
<script src="app.js"></script> 
<script src="customers.js"></script> 
<script src="products.js"></script> 
</html> 

路由客戶頁面customers.js

angular.module('storeApp').config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider){ 
     $routeProvider. 
     when("/customer", { 
      templateUrl: "/customer.html" 
      //controller: 'customerCtrl', 
      //controllerAs: 'vm' 
     }). 
     otherwise({ 
      redirectTo: '/main' 
     }); 

     $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest'; 

    }]).controller('customerCtrl', customerCtrl); 

WebSecurityConfig.java

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .csrf().disable() 
      .authorizeRequests() 
       .antMatchers("/", "/customer.html", "/index.html", "/main.html", "/*.js", "/user", "/logout").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/customer.html") 
       .permitAll() 
       .and() 
      .httpBasic() 
       .and() 
      .logout() 
       .permitAll(); 
    } 

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

the

.formLogin() 
        .loginPage("/customer.html") 
        .permitAll() 
        .and() 

是我遇到我的問題。

當用戶試圖訪問需要在角度頁面上登錄的頁面時,該頁面可以正常工作,並重定向到正確的路由,我將其作爲/#/ customer。但是,當您嘗試訪問不在匹配器中的非角度頁面時,as/asdfasdf會轉到原始html頁面,而不是角度路徑。

我在想什麼是,有什麼辦法使WebSecurityConfig工作與角度路線?如果沒有,解決這個問題的方法是什麼?

+0

這個看看:http://stackoverflow.com /問題/ 23919578/angularjs-Web的應用程序使用彈簧安全 – Shota

回答

0

我覺得你可以在類似於路線的頁面沒有找到現有頁面的web.xml配置的東西,例如登錄頁面

<error-page> 
    <error-code>404</error-code> 
    <location>/index.html</location> 
</error-page>