授权使用Spring Security和AngularJS访问资源



我允许在此代码中显示的弹簧安全中访问资源:"然后用户身份验证是从db"

@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'
        });  
});

我的问题是如何建立访问授权的链接 用Angularjs的URL在春季安全性中定义($ RouteProvider(

谢谢,并过得愉快,

您可以尝试启用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)
});

我不确定这是否满足您的要求,但是是的,我已经使用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"])  
            }]
        }
    });

有关更多详细信息,请单击此处

最新更新