【问题标题】:Conflict between Spring Security and AngularJS routingSpring Security 和 AngularJS 路由之间的冲突
【发布时间】:2016-03-06 22:10:57
【问题描述】:

我目前遇到了 angularJS 路由和 Spring Security 默认登录表单之间的冲突问题。如果我将 Angular 路由作为登录页面,它不喜欢这样,并且似乎开始了无限重定向循环。

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

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

是我遇到问题的地方。

当用户尝试访问需要登录的页面时在角度页面上,它可以正常工作并重定向到正确的路线,我将其作为 /#/customer.但是,当您尝试访问不在匹配器中的非角度页面时,例如 /asdfasdf,它会转到原始 html 页面,而不是角度路径。

我想知道的是,有没有办法让 WebSecurityConfig 与角度路由一起工作?如果没有,有什么方法可以解决这个问题?

【问题讨论】:

标签: javascript java angularjs spring spring-security


【解决方案1】:

我认为您可以在 web.xml 中配置类似于将找不到的页面路由到现有页面,例如登录页面

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

【讨论】:

    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多