2017-07-28 7 views
0

私はspring.Iに新しいですが、login.Theユーザーログインの方法は、コードを次のものが含まれ、ユーザーのためのAPIを作成しました:春の式ベースのアクセス制御の使い方は?

Authentication auth =new UsernamePasswordAuthenticationToken(repo.findByEmail(user.getEmail()), null, repo.findByEmail(user.getEmail()).getAuthorities()); 

SecurityContextHolder.getContext().setAuthentication(auth); 
Principal principal= SecurityContextHolder.getContext().getAuthentication(); 
return (User) auth.getPrincipal(); 

ログインAPIは私に次のようなJSONを与える:

{ 
"userId": 1, 
"userName": "ramesh khadka", 
"email": "[email protected]", 
"enabled": true, 
"role": "Manager", 
"username": "ramesh khadka", 
"authorities": [ 
    { 
     "authority": "ROLE_Manager" 
    } 
], 
"accountNonExpired": true, 
"accountNonLocked": true, 
"credentialsNonExpired": true 
} 

私が持っています別のAPIが示されている、私がメッセージ以下のAPIを呼び出すgranted.When当局をチェックする@PreAuthorize(「hasAuthority( 『ROLE_Managerを』)」)を使用してすべてのユーザーを取得します -

{ 
"error": "unauthorized", 
"error_description": "Full authentication is required to access this resource" 
} 

私は何をしているのか分かりません。誰かが私に助けてくれますか?

マイAuthorizationServerがある: -

@Configuration 
@EnableAuthorizationServer 
public class SecurityServer extends AuthorizationServerConfigurerAdapter { 
@Autowired 
@Qualifier("userDetailsService") 
private UserDetailsService userDetailsService; 

@Autowired 
private AuthenticationManager authenticationManager; 

@Override 
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
    security.checkTokenAccess("isAuthenticated()"); 
} 

@Override 
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
    // super.configure(clients); 
    clients.inMemory() 
      .withClient("test") 
      .accessTokenValiditySeconds(3600) 
      .authorizedGrantTypes("authorization_code","refresh_token","password","client_credentials") 
      .secret("user") 
      .scopes("read", "write", "trust") 
      .redirectUris("/error") 
      .resourceIds("resource"); 
} 

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints.authenticationManager(this.authenticationManager); 
    endpoints.userDetailsService(userDetailsService); 
} 
@Bean 
public PasswordEncoder passwordEncoder() { 
    return new BCryptPasswordEncoder(); 
} 
@Bean 
public FilterRegistrationBean corsFilter() { 
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    CorsConfiguration config = new CorsConfiguration(); 
    config.setAllowCredentials(true); 
    config.addAllowedOrigin("*"); 
    config.addAllowedHeader("*"); 
    config.addAllowedMethod("*"); 
    source.registerCorsConfiguration("/**", config); 
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); 
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE); 
    return bean; 
} 

}

+0

あなたは春のセキュリティ設定を追加することができます。あなたはこれらのリンクを参照することが多くのための

? –

+0

@AnupamaBoorlagadda私は私のセキュリティコードを含んでいます –

答えて

0

あなたのコードで説明したように、あなたが使用することを持っている '@PreAuthorize( "hasAuthority( 'ROLE_Manager')")'

hasAuthority([ authority]) - >現在のプリンシパルに指定された権限がある場合はtrueを返します。

「@PreAuthorize( "hasRoleも( 'マネージャー')") `

@PreAuthorize("hasRole('USER')") 
public void create(Contact contact); 

どちらかあなたはこれがあなた は hasAnyRoleを確認し、その後を探している彼らのいずれかの役割であるために使用することができます([ROLE1を追加してください、role2]) - >現在のプリンシパルに指定されたロール(カンマで区切られた文字列のリスト)がある場合はtrueを返します。デフォルトでは、指定されたロールが 'ROLE_'で始まらない場合、追加されます。これは、DefaultWebSecurityExpressionHandlerのdefaultRolePrefixを変更することでカスタマイズできます。 https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html https://dzone.com/refcardz/expression-based-authorization

関連する問題