为角度js应用增加弹簧安全性而苦苦挣扎

时间:2016-01-31 20:09:42

标签: angularjs spring spring-mvc spring-security

我正在尝试从spring项目中的角度js配置登录视图。下面是我有的spring security xml设置..

<http auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint">
      <intercept-url pattern="/**" access="ROLE_ADMIN"/>
        <custom-filter position="FORM_LOGIN_FILTER" ref="theiaFormAuthenticationFilter"/>
      <custom-filter ref="theiaRestDigestFilter" after="FORM_LOGIN_FILTER" />
      <logout logout-success-url="/#/login" />
      <session-management invalid-session-url="/#/login" />
      <access-denied-handler error-page="/loginFailed"/>
</http>

每当我尝试去ERR_TOO_MANY_REDIRECTS的任何页面时,它都会给我localhost/#/login。请帮忙。自从过去两天以来一直在努力工作,没有去过哪里。

谢谢。

更新

实际上现在我改变了代码以使用Spring安全配置类。它能够提供弹簧安全性附带的默认登录页面。但是我无法为登录添加angularjs路由。以下是安全性代码:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("bill").password("abc123").roles("USER");
    auth.inMemoryAuthentication().withUser("admin").password("root123").roles("ADMIN");
    auth.inMemoryAuthentication().withUser("dba").password("root123").roles("ADMIN","DBA");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    log.info("Configuring theia security settings.");
    http.authorizeRequests()
         .antMatchers("/libs/**").permitAll()
         .anyRequest().authenticated()
        .and()
        .formLogin()
            .loginPage("/#/login").permitAll()
        .and()
        .logout().permitAll();
}
}

似乎无法识别/#/ login。在这个应用程序中,我们没有索引页面。它必须直接进入登录页面。无论如何在配置文件中添加角度路由?我错过了什么吗?

我现在正在关注本教程:https://www.youtube.com/watch?v=1zu8COg80q4

1 个答案:

答案 0 :(得分:0)

您有本教程..您将为此目的提供的最佳教程。它从简单的应用程序一步步走向最复杂的oauth2和splitted服务器。非常推荐: https://spring.io/guides/tutorials/spring-security-and-angular-js/

编辑: 因为你正在使用angularjs你不能做你正在做的事情。 Spring安全性只会节省资产而不会监听网址。 这是单页面应用程序,因此Webflow上的所有控件都应该在客户端上。

你应该使用类似的东西:

http
        .httpBasic()
      .and()
        .authorizeRequests()
          .antMatchers("/index.html", "/home.html", "/login.html").permitAll()
          .anyRequest().authenticated();

因此,当客户端未经许可尝试获取资源时,您不会为您提供资源。在客户端中,您需要执行路由逻辑。

实际上是这样的:

angular.module('hello', [ 'ngRoute' ]) // ... omitted code
.controller('navigation',

  function($rootScope, $scope, $http, $location) {

  var authenticate = function(credentials, callback) {

    var headers = credentials ? {authorization : "Basic "
        + btoa(credentials.username + ":" + credentials.password)
    } : {};

    $http.get('user', {headers : headers}).success(function(data) {
      if (data.name) {
        $rootScope.authenticated = true;
      } else {
        $rootScope.authenticated = false;
      }
      callback && callback();
    }).error(function() {
      $rootScope.authenticated = false;
      callback && callback();
    });

  }

  authenticate();
  $scope.credentials = {};
  $scope.login = function() {
      authenticate($scope.credentials, function() {
        if ($rootScope.authenticated) {
          $location.path("/");
          $scope.error = false;
        } else {
          $location.path("/login");
          $scope.error = true;
        }
      });
  };
});

在服务器中,当您尝试发送get / user时,如果用户会话处于活动状态,服务器将返回会话用户。所以在服务器中你还需要:

@SpringBootApplication
@RestController
public class UiApplication {

  @RequestMapping("/user")
  public Principal user(Principal user) {
    return user;
  }

  ...

}