2014-09-24 16 views
1

我正在使用彈簧安全的彈簧mvc項目中工作,我是Spring安全新手,我想知道如何讓我的兩種類型的用戶應用程序,普通用戶和管理員用戶,並顯示不同的索引頁面,管理員用戶與正常用戶較少功能的另一個索引頁,到目前爲止,我有這樣的:如何在彈簧安全中顯示不同類型的用戶頁面

我configSecurity類WebSecurityConfigurerAdapter

public class ConfigSecurity extends WebSecurityConfigurerAdapter { 

private AutenticarProvider authen; 


    @Override 
    protected void configure(HttpSecurity http) throws Exception 
    { 
     http 
      .authenticationProvider(authen) 
      .authorizeRequests() 
        .antMatchers("/resources/**").permitAll() 
        .antMatchers("/css/**").permitAll() 
        .antMatchers("/js/**").permitAll() 
        .antMatchers("/img/**").permitAll() 
        .antMatchers("/sound/**").permitAll() 
        .antMatchers("/fonts/**").permitAll() 
        .antMatchers("/ajax/**").permitAll() 
        .antMatchers("/php/**").permitAll() 
        .antMatchers("/xml/**").permitAll() 
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") <-- i am not sure about this just guessing 
        .anyRequest().authenticated() 
        .and() 
      .formLogin() 
        .loginPage("/loginPage") 
        .permitAll() 
        .and() 
      .logout()          
        .permitAll(); 
    } 
} 

而我的課實現AuthenticationProvider:

@Component 
public class AutenthenProvider implements AuthenticationProvider 
{ 
public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException { 

     User user = null; 
     Authentication auth = null; 
     String name = null; 
     String password = null; 

     try 
     { 
      name = authentication.getName(); 
      password = authentication.getCredentials().toString(); 

      if(name != null && !name.trim().equals("") && password != null && !password.trim().equals("")) 
      { 
       user = this.obtainUserFromDataBase(name); 

       if(user != null) 
       { 
        List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 



        auth = new UsernamePasswordAuthenticationToken(name, password); 
       } 
       else 
       { 
        throw new UsernameNotFoundException("the user dont exist"); 

       } 

      } 
      else 
      { 
       throw new BadCredentialsException("invalid credentials"); 
      } 
     } 
     catch (AuthenticationException e) { 
      throw e; 
     } 
     catch (Exception ex) { 
      throw new AuthenticationServiceException("", ex.getCause()); 
     } 

     return auth; 
    } 

,並從我的控制器類

@RequestMapping(value = "/loginPage", method = RequestMethod.GET) 
    public String loginPage(Model model) { 

     logger.info("**Login PAGE!!**"); 

     return "loginPage"; 
    } 

我想到把這個線.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")在我的配置方法,但我不知道這是如何工作我控制器的方法,如果我把那個意思,我我將會有重複的頁面,因爲我的應用程序中有可以被兩個用戶查看的頁面這是否意味着我將把這兩個頁面複製到不同的文件夾中?

回答

0

您可以使用tags that Spring has builtin作爲基於角色的目的,例如,管理員或用戶。您還可以定義自定義角色。

<sec:authorize access="hasRole('supervisor')"> 

This content will only be visible to users who have 
the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s. 

</sec:authorize> 

您還可能有與hasRole([role]) method用於基於代碼的解決方案運氣還是看看這個答案How to check "hasRole" in Java Code with Spring Security?

+1

我有一個基於代碼的配置和我發現的所有教程都是爲xml配置你有一個基於代碼的教程也許你可以給我嗎? – stackUser2000 2014-09-24 21:20:22

+0

我想你可以使用'hasRole([role])'方法(但我沒有自己嘗試) – 2014-09-25 05:28:41

0

首先,正確的方法是使用角色。在你AuthenticationProvider,你給GrantedAuthorityROLE_ADMIN admin用戶,通過稍微修改它:

List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 

// only if user is recognized as admin 
    grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN") 

auth = new UsernamePasswordAuthenticationToken(name, password); 

然後,你將能夠使用.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")限制訪問/admin/** admin用戶,你可以把在JSP所提議的909人Niklas

反正

<sec:authorize access="hasRole('ROLE_ADMIN')"> 

This content will only be visible to users who have 
the "ROLE_ADMIN" authority in their list of <tt>GrantedAuthority</tt>s. 

</sec:authorize> 

頁,我幾乎從來沒有實現一個AuthenticationProvider。當用戶存儲在真實數據庫中時,我通常使用DaoAuthenticationProviderUserDetailsServiceInMemoryUserDetailsManager進行測試,JdbcUserDetailsManager)。