简体   繁体   English

登录后Spring Security + Angularjs重定向

[英]Spring Security + Angularjs redirect after login

Trying to use idea from this tutorial: https://spring.io/guides/tutorials/spring-security-and-angular-js/ 尝试使用本教程中的思想: 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();
    }

Also I use angularjs ngRoute: 我也使用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: '/'
        });
}]);

And so on. 等等。

But when I try to open admin page (for example /#/admin) it opens login form (from the box) and after that I expect that it should redirect me back to /#/admin, but it redirect me to /admin.html, which is out of the angular control. 但是,当我尝试打开管理页面(例如/#/ admin)时,它会打开登录表单(从框中),然后我希望它可以将我重定向回/#/ admin, 但会将我重定向到/ admin。 html,这超出了角度控制范围。

How to solve this? 如何解决呢?

To address the /error redirection you can create a controller like this 为了解决/错误重定向,您可以创建一个像这样的控制器

@Controller
public class ErrorController {


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

}

from a spring-boot perspective it should always fall in index.html where you are "giving the control to angular" 从春季启动的角度来看,它应该始终落在“将控件赋予角度”的index.html中

Im assuming your resources folder structure is as follows resources/static/index.html 我假设您的资源文件夹结构如下resources / static / index.html

so that index gets rendered when accesing root. 以便在访问root时呈现该索引。

My directory structure is diffent than the one on the tutorial but it should work 我的目录结构与教程中的目录结构不同,但是应该可以使用

Just Understood what your problem is: So in your angular config add this : 刚了解了您的问题是:因此,在您的角度配置中添加以下内容:

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

and in the index.htm include the base tag 并在index.htm中包含基本标记

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

this will get rid of the '#' charecter and everything should work 这将摆脱“#”字符,一切都应该工作

Hope it helps 希望能帮助到你

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

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