2016-11-22 131 views
0

當用戶點擊http://localhost:8080時,我有要求通過春季安全顯示基於自定義的登錄表單(/auth/login.html)。如果用戶以管理員角色成功登錄,請將用戶重定向到/admin/adminsuccess.html。一旦admin用戶重定向到/adminsuccess.html,我需要允許admin用戶訪問其他頁面,例如(/admin/assetallocate.html,/admin/assetdeallocate.html..)If用戶不具有管理員的角色登錄,顯示有錯誤相同的登錄頁面..春季安全螞蟻匹配家庭根/ - 春季啓動1.4.2發佈版本

下面是我的代碼:

@Configuration 
    public class AssetWebConfig extends WebMvcConfigurerAdapter { 

     @Override 
     public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/login").setViewName("auth/login"); 
     registry.addViewController("/admin/adminsuccess").setViewName("admin/auth-success"); 
     registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
    } 

    } 

    @Configuration 
    @EnableWebSecurity 
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
     .authorizeRequests() 
     .antMatchers("/login").permitAll() 
     .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") 
     .antMatchers("/**").access("hasRole('ROLE_ADMIN')") 
      .and() 
     .formLogin() 
      .loginPage("/login") 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .defaultSuccessUrl("/admin/adminsuccess") 
      .and() 
     .logout() 
      .logoutSuccessUrl("/login?logout") 
      .permitAll() 
      .and() 
      .csrf().disable(); 
} 

}

/auth/login.html 
    <form name="loginform" th:action="@{/login}" method="post" class="form-signin"> 

上面的代碼無論我寫不符合預期的工作。這可能是螞蟻匹配模式的問題。請指導。

更新: 當我點擊「http://localhost:8080」時,自定義登錄頁面正在顯示。但是,當我輸入正確的憑據時,它不會重定向到基於AssetWebConfig.java配置查看名稱「/admin/auth-success.html」。如果我輸入正確的憑證,下面是當前的迴應。

This application has no explicit mapping for /error, so you are seeing this as a fallback. 
Wed Nov 23 11:42:59 IST 2016 
There was an unexpected error (type=Not Found, status=404). 
No message available 

回答

0

是的。問題與你的螞蟻匹配者。

按我的理解,當你說anyRequest.permitAll,它不符合antMatchers( 「/管理/ *」)。訪問( 「hasRole( 'ROLE_ADMIN')」) 因爲你'告訴網絡安全,允許所有請求在未經授權的情況下通過。

更改如下,

http.authorizeRequests() 
     .antMatchers("/login").permitAll() 
     .antMatchers("/admin/**").access("hasRole('ADMIN')") 
     .and().formLogin().loginPage("/login") 

https://github.com/satya-j/rab/blob/master/src/main/java/com/satya/rab/config/WebSecurityConfig.java - 參閱本,其回購我,我早些時候曾在Spring Security嘗試了。

編輯:

這裏是一個更新

WebSecurityConfig

.antMatchers("/login").permitAll() 
.antMatchers("/admin/**").access("hasRole('ADMIN')") 
.antMatchers("/**").access("hasRole('USER')") 
.and() 
.formLogin().loginPage("/login") 
.usernameParameter("username") 
.passwordParameter("password") 
.defaultSuccessUrl("/index") 
.failureUrl("/login?error"); 

您可以使用自己選擇的設置基於用戶角色的身份驗證提供者。

CustomeAuthenticationProvider

@Component("authProvider") 
public class CustomAuthenticationProvider implements AuthenticationProvider  { 

@Override 
public Authentication authenticate(Authentication auth) throws AuthenticationException { 
    String username = auth.getName(); 
    String password = auth.getCredentials().toString(); 

    if(username.equals("user") && password.equals("user")) { 
     List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 
     grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
     return new UsernamePasswordAuthenticationToken(username, password, grantedAuths); 
    } else if(username.equals("admin") && password.equals("admin")) { 
     List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>(); 
     grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
     grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
     return new UsernamePasswordAuthenticationToken(username, password, grantedAuths); 
    } else { 
     throw new CustomException("Unable to auth against third party systems"); 
    } 
} 

@Override 
public boolean supports(Class<?> auth) { 
    return auth.equals(UsernamePasswordAuthenticationToken.class); 
} 

我已經使用了自定義身份驗證。當我玩彈簧安全時,我沒有去任何數據庫配置。你可以用你自己的方式來實現它。以上驗證了授權憑證並設置了角色(權限)。由於管理員也可以查看用戶模塊(大多數情況下,至少這是我的概念),所以當管理員登錄時,我已將管理員用戶& admin連接起來。簡而言之, 1.當用戶登錄時,他會能夠訪問每個/ **,但不能訪問/ admin/** 2。當管理員日誌中,他就可以訪問每一個/ **和/管理/ **

我測試過的場景,整個代碼,你可以去這裏雖然 - https://github.com/satya-j/rab

+0

它不工作: ( – user2057006

+0

你可以發佈你的服務器日誌或堆棧跟蹤,如果你得到一個異常 –

+0

我編輯我的帖子與所需的細節。請幫助。 – user2057006