繁体   English   中英

登录后Spring Security + Angularjs重定向

[英]Spring Security + Angularjs redirect after login

尝试使用本教程中的思想: https : //spring.io/guides/tutorials/spring-security-and-angular-js/

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().and()
            .logout().and().authorizeRequests()
            .antMatchers("/index.html", "/main.html", "admin.html",  
            "/login.html", "/")         
            .permitAll().anyRequest()
            .authenticated().and().csrf().disable();
    }

我也使用angularjs ngRoute:

var myApp = angular.module('myApp',['ngRoute']);

myApp.config(['$routeProvider',
    function($routeProvider) {
      $routeProvider
        .when('/', {
          templateUrl: 'main.html',
              controller: 'MainController'
        })
        .when('/admin', {
          templateUrl: 'admin.html',
              controller: 'AdminController'
        })
        .when('/login', {
          templateUrl: 'login.html',
          controller: 'LoginController'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

等等。

但是,当我尝试打开管理页面(例如/#/ admin)时,它会打开登录表单(从框中),然后我希望它可以将我重定向回/#/ admin, 但会将我重定向到/ admin。 html,这超出了角度控制范围。

如何解决呢?

为了解决/错误重定向,您可以创建一个像这样的控制器

@Controller
public class ErrorController {


    @RequestMapping(value="/error")
    public String error() {
        return "forward:/index.html";
    }

}

从春季启动的角度来看,它应该始终落在“将控件赋予角度”的index.html中

我假设您的资源文件夹结构如下resources / static / index.html

以便在访问root时呈现该索引。

我的目录结构与教程中的目录结构不同,但是应该可以使用

刚了解了您的问题是:因此,在您的角度配置中添加以下内容:

$locationProvider.html5Mode(true).hashPrefix('#');

并在index.htm中包含基本标记

  <base href="<!-- @echo BASE_URL -->" target="_blank">

这将摆脱“#”字符,一切都应该工作

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM