2017-04-26 96 views
0

的我发权限访问在弹簧安全资源如本代码:“用户认证是然后从DB”授权访问资源与弹簧安全和Angularjs

@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保持网址跟踪:

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的网址($ routeProvider)

谢谢 和有一个好的一天,

定义的访问权限的链接
+1

有很多的教程围绕关于单页的应用程序的用户授权。先研究它们。问题太广泛了,因为你已经开始有很大的差异 – charlietfl

回答

0

你可以尝试让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) 
}); 
+1

这完全误导了OP,当他们对SPA中的整个安全过程存在根本的误解时,他们可以做出简单的改变 – charlietfl

+0

请注意,当用户切换角度路线时提供**零**保护,并且答案没有解释客户端授权如何工作 – charlietfl

+0

@leguest感谢您的回答。他有点解决了这个问题。但是当我在标签“a”( Users)中指定url时,它会重定向到“** http:// localhost:8080/Users **”,但我必须拥有此URL“** http:// localhost:8080/App/Users **“您有一个说明性教程,以确保用户的授权和安全 – Michael1

0

我不确定这会符合您的要求,但是我已经使用过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

+0

谢谢,我尽量不授权使用angularJs进行访问,但Spring安全性更好。我只允许具有admin角色的用户通过spring安全中定义的url“/ Users”来管理用户。那么我可以在angularjs中定义这个吗? – Michael1

+0

根据@leguest,他是对的,我们可以按照他的指示通过启用html5mode来尝试 –