【问题标题】:Authorization of access to resources with spring security and Angularjs使用 spring security 和 Angularjs 授权访问资源
【发布时间】:2017-09-23 17:32:24
【问题描述】:

我在spring security中获得了访问资源的权限,如下代码所示:“用户身份验证来自数据库”

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
     @Autowired
     protected void globalConfig(AuthenticationManagerBuilder auth, DataSource dataSource) throws Exception {
     //auth.inMemoryAuthentication().withUser("user").password("123").roles("USER");
         auth.jdbcAuthentication()
             .dataSource(dataSource)
             .usersByUsernameQuery("select username as principal, password as credentials, etat as actived from utilisateurs where username=?")
             .authoritiesByUsernameQuery("select u.username as principal, ur.nom_role as role from utilisateurs u inner join roles ur on(u.roles_id=ur.id_role) where u.username=?")
             .rolePrefix("ROLE_");
     }

 protected void configure(HttpSecurity http) throws Exception {
          http
         .csrf().disable()
           .sessionManagement().maximumSessions(100).maxSessionsPreventsLogin(false).expiredUrl("/Login");
          http
           .authorizeRequests()
           .antMatchers("/AppJS/**","/images/**","/pdf/**","/Template/**","/Views/**","/MainApp.js").permitAll()
           .antMatchers("/Users/**").access("hasRole('ADMIN')")
           .antMatchers("/Dashbord/**").access("hasRole('ADMIN')")
           .antMatchers("/Login*").anonymous()
           .anyRequest().authenticated()
           .and()
         .formLogin().loginPage("/Login").permitAll()
           .defaultSuccessUrl("/home")
           .failureUrl("/Login?error=true")
           .and().exceptionHandling().accessDeniedPage("/Access_Denied")
           .and()
         .logout()
            .invalidateHttpSession(true)
            .clearAuthentication(true)
            .logoutUrl("/logout")
            .permitAll()
           .logoutSuccessUrl("/Login");


     }

}

随后,我为每个 URL 指定了视图:

@Configuration
public class MvcConfig  extends WebMvcConfigurerAdapter{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
      registry.addViewController("/Login").setViewName("Login");
       registry.addViewController("/Dashbord").setViewName("home");
       registry.addViewController("/logout").setViewName("Login");
       registry.addViewController("/Users").setViewName("Views/ListUsers");
    }
}

我使用 angularJS routeProvider 来跟踪 URL:

var app = angular.module('Mainapp', ['ngRoute','file-model','ui.bootstrap','ngMessages']);
app.config(function($routeProvider) {
    $routeProvider
        .when('/Users', {
                controller:'UsersController', 
                templateUrl: 'Views/ListUsers'
        })     
      .when('/Dashbord', {
              controller: 'ResultController',
             templateUrl: 'Views/home.html'
        });  
});

我的问题是如何制作我的访问授权的链接 在 spring security 中使用 angularjs ($ 路由提供者)

谢谢你, 祝你有美好的一天,

【问题讨论】:

  • 有很多关于单页应用程序中的用户授权的教程。首先研究它们。问题过于广泛,因为与您已经开始的内容存在重大差异

标签: java angularjs spring-mvc spring-security


【解决方案1】:

您可以尝试启用html5mode,以获得此功能

AngularJS: http://localhost:8080/Users

app.config(function($routeProvider, $locationProvider) {
   $routeProvider
    .when('/Users', {
            controller:'UsersController', 
            templateUrl: 'Views/ListUsers'
    })     
  .when('/Dashbord', {
          controller: 'ResultController',
         templateUrl: 'Views/home.html'
    });  

   $locationProvider.html5Mode(true)
});

【讨论】:

  • 这完全误导了 OP,让他们相信当他们对 SPA 的整个安全过程有根本的误解时,可以进行简单的更改
  • 请注意,当用户切换角度路线时,这将提供 保护,并且回答无法解释客户端授权如何工作
  • @leguest 谢谢你的回答。他解决了这个问题。但是当我在标签“a”( Users )中指定 url 时,它会重定向到“http://localhost:8080 / Users”但是我必须有这个Url“http://localhost:8080/App/Users”你有说明教程保证用户的授权和安全
【解决方案2】:

我不确定这是否符合您的要求,但是我之前已经使用ngPermission 做过。在此之前,您需要在路线中设置角色列表。

.state('view1', {
        templateUrl: 'view1/view1.html',
        controller: 'View1Ctrl',
        resolve: {
            authorization: ["ngPermissionService", function (ngPermissionService) {

                //you need to call webserivce at this level for get all user's permissions and return it.

                return ngPermissionService.role(["admin"])  

            }]
        }
    });

更多详情click here

【讨论】:

  • 谢谢,我尽量不使用angularJs授权访问,而是使用spring security更好。我只允许具有管理员角色的用户通过 spring security 中定义的 url "/Users" 对用户进行管理。那么我可以在 angularjs 中定义它吗?
  • 按照@leguest的说法,他是对的,我们可以按照他的指示启用html5mode,试试看
猜你喜欢
  • 2015-10-09
  • 2018-10-11
  • 2016-09-29
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 2021-10-29
  • 1970-01-01
相关资源
最近更新 更多