2015-05-29 47 views
0

嗨我是新來的春天,我想了解更多一點安全模型。 在我們的項目中,我們控制它的方法標註有從@PreAuthorize的whereRole在Spring中取其值嗎?

@PreAuthorize("hasRole('REPORT_VIEW')") 

所以我從這個地方來REPORT_VIEW從是ENUM或者是基於一些xml配置疑惑?我在文件中搜索,但我找不到REPORT_VIEW作爲單詞。

從這裏: http://www.mkyong.com/spring-security/spring-security-access-control-example/

我看到有配置安全的context.xml其中

<user-service> 
     <user name="mkyong" password="password" authorities="ROLE_USER" /> 
     <user name="eclipse" password="password" authorities="ROLE_ADMIN" /> 
</user-service> 

在我的項目

<sec:authentication-manager> 
     <sec:authentication-provider user-service-ref="clientDetailsUserService" /> 
</sec:authentication-manager> 

<bean id="clientDetailsUserService" 
     class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"> 
     <constructor-arg ref="clientDetails" /> 
</bean> 

所以我作爲開發誰想要創建一些控制器從哪裏瞭解哪些是應用程序的角色?

回答

1

我可以通過XML文件和一些Java代碼來解釋你。以下是我如何登錄並分配角色。您還可以查詢數據庫中的角色。

安全的applicationContext.xml:

<security:authentication-manager alias="authenticationManager"> 
     <security:authentication-provider user-service-ref="LoginServiceImpl"> 
      <security:password-encoder ref="encoder"/> 
     </security:authentication-provider> 
    </security:authentication-manager> 


    <beans:bean id="daoAuthenticationProvider" 
       class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
       <beans:property name="userDetailsService" ref="LoginServiceImpl"/> 
       <beans:property name="passwordEncoder" ref="encoder"/> 
    </beans:bean> 

以上是我的數據庫認證碼,其referes到LoginServiceImpl的豆,從那裏我正在尋找在DB用戶。模型類已經實現了UserDetails。

LoginServiceImpl:

@Transactional 
@Service("userDetailsService") 
public class LoginServiceImpl implements UserDetailsService{ 

    @Autowired private PersonDAO personDAO; 
    @Autowired private Assembler assembler; 

    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException,DataAccessException { 
     Person person = personDAO.findPersonByUsername(username.toLowerCase()); 
      if(person == null) { throw new UsernameNotFoundException("Wrong username or password");} 
     return assembler.buildUserFromUserEntity(person); 
    } 

    public LoginServiceImpl() { 
    } 
} 

當用戶在數據庫中,我構建可通過彈簧安全的會議,所有使用的對象。這是怎麼一回事呢:

@Service("assembler") 
public class Assembler { 
    @Transactional(readOnly = true) 
    User buildUserFromUserEntity(Person userEntity){ 
     String username = userEntity.getUsername().toLowerCase(); 
     String password = userEntity.getPassword(); 

     boolean enabled = userEntity.isEnabled(); 
     boolean accountNonExpired = userEntity.isAccountNonExpired(); 
     boolean credentialsNonExpired = userEntity.isCredentialsNonExpired(); 
     boolean accountNonLocked = userEntity.isAccountNonLocked(); 

     Collection<GrantedAuthority> authorities = new ArrayList<>(); 
     authorities.add(new SimpleGrantedAuthority("ROLE_USER")); 

     return new User(username,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked,authorities); 
     } 
} 

正如你所看到的,我加入的角色,但我還可以查詢數據庫,或者你發現任何其他邏輯,然後拿過來,用戶將有那個角色。理想情況下,Person模型類應該具有到Role類的一對多映射以及其中的用戶角色。 如果這不是你要找的,我會刪除我的答案,只是讓我知道。

1

春季安全的功能也不是那麼微不足道,但我試着解釋:

Spring Security認證是基於org.springframework.security.core.userdetails.UserDetails這是一個用戶的春天抽象。 A UserDetailsGrantedAuthority,它可以是應用程序的權限,角色或任何其他「SecurityItem」。 hasRole檢查當前用戶是否在表達式中具有名稱爲GrantedAuthority(例如'REPORT_VIEW')。

但這些UserDetails從哪裏來。這取決於你的應用程序。由實施org.springframework.security.core.userdetails.UserDetailsService的課程提供。這就是你的應用程序的特定用戶存儲所在的地方.Spring提供了一些實現方式UserDetailsService,但你也可以實現你自己的(例如,如果你將用戶憑證存儲在自定義數據庫中)。

在您鏈接的示例中,UserDetailsService是基於XML(最簡單的變體)創建和配置的。在您的應用程序中,通過OAuth2提供了UserDetails,並且在您的應用程序中定義了您沒有提供的名爲clientDetails的bean。

相關問題