【问题标题】:Struggling with adding spring security for angular js application努力为 Angular js 应用程序添加弹簧安全性
【发布时间】:2016-05-09 05:10:55
【问题描述】:

我正在尝试在 spring 项目中从 angular 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 安全配置类。它能够提供 Spring Security 附带的默认登录页面。但我无法添加用于登录的 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

【问题讨论】:

  • 'theiaFormAuthenticationFilter' 和 'loginUrlAuthenticationEntryPoint' 的定义是什么?
  • 您调用哪个 url,以及重定向到哪个 url? (尝试排除受保护的登录网址,还要考虑资源(js、css、图像...))
  • 实际上我现在更改了代码以使用 Spring 安全配置类。它能够提供 Spring Security 附带的默认登录页面。但我无法添加用于登录的 angularjs 路由。以下是安全代码:

标签: angularjs spring spring-mvc spring-security


【解决方案1】:

你有这个教程..你将为此目的最好的教程。它一步一步从简单的应用程序到最复杂的 oauth2 和拆分服务器。非常推荐: https://spring.io/guides/tutorials/spring-security-and-angular-js/

编辑: 因为你使用的是 angularjs,所以你不能做你正在做的事情。 Spring security 只会节省资产而不听 url。 这是单页应用程序,因此 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;
  }

  ...

}

【讨论】:

  • 我看过这个教程。但是你能检查一次我的更新吗?角度 js 路线似乎没有被识别!每当我尝试访问 localhost/#/login 时,它都会进入 404 页面
  • 查看我的编辑。我把重要的事情..理解简单的事情..因为angularjs并不是真正不同的页面,而是一个页面,你不能说spring重定向到另一个页面。所有逻辑路由逻辑都应该在客户端。您只能保护服务器上的资源,以保护将尝试访问受限区域的经过身份验证的用户
  • 感谢 Aviad。我只是通过更改 AngularJS UI 端的身份验证来让它工作。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2018-12-16
  • 2019-02-12
  • 1970-01-01
  • 2014-10-27
  • 2023-01-03
  • 2013-01-25
  • 2012-01-07
相关资源
最近更新 更多