我正在使用彈簧安全的彈簧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')")
在我的配置方法,但我不知道這是如何工作我控制器的方法,如果我把那個意思,我我將會有重複的頁面,因爲我的應用程序中有可以被兩個用戶查看的頁面這是否意味着我將把這兩個頁面複製到不同的文件夾中?
我有一個基於代碼的配置和我發現的所有教程都是爲xml配置你有一個基於代碼的教程也許你可以給我嗎? – stackUser2000 2014-09-24 21:20:22
我想你可以使用'hasRole([role])'方法(但我沒有自己嘗試) – 2014-09-25 05:28:41