Java 如何在spring security中为某种类型的用户显示不同的页面

Java 如何在spring security中为某种类型的用户显示不同的页面,java,spring,spring-mvc,login,spring-security,Java,Spring,Spring Mvc,Login,Spring Security,我在一个使用spring security的spring mvc项目中工作,我是spring security的新手,我想知道如何在我的应用程序中有两种类型的用户,一个普通用户和一个管理员用户,并向管理员用户显示不同的索引页,另一个向普通用户显示功能较少的索引页,到目前为止,我有以下几点: 我的configSecurity类WebSecurity配置适配器 public class ConfigSecurity extends WebSecurityConfigurerAdapter { pr

我在一个使用spring security的spring mvc项目中工作,我是spring security的新手,我想知道如何在我的应用程序中有两种类型的用户,一个普通用户和一个管理员用户,并向管理员用户显示不同的索引页,另一个向普通用户显示功能较少的索引页,到目前为止,我有以下几点:

我的configSecurity类WebSecurity配置适配器

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();
    }
}
我正在考虑将这一行
.antMatchers(“/admin/**”).access(“hasRole('ROLE\u admin')”)
放在我的配置方法中,但我不确定这是如何工作的,如果我把它放进去,这是否意味着我将有重复的页面,因为我的应用程序中有两个用户都可以查看的页面?这是否意味着我将有两个页面重复,但放在不同的文件夹中?

您可以根据角色(例如管理员或用户)使用。您还可以定义自定义角色

<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>

此内容仅对具有以下权限的用户可见:
“主管”权限在其授权权限列表中。

您可能还幸运地找到了基于代码的解决方案,或者看看这个答案。首先,正确的方法是使用角色。在您的
AuthenticationProvider
中,您可以通过稍微修改以下内容,将
授予权限
角色_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);
由909 Niklas提议


无论如何,我几乎从未实现过
AuthenticationProvider
。我通常使用
DAAuthenticationProvider
和相关的
UserDetailsService
InMemoryUserDetailsManager
用于测试,而
JdbcUserDetailsManager
用于将用户存储在真实数据库中).

我有一个基于代码的配置,我找到的所有教程都是针对xml配置的。你有基于代码的教程吗?也许你可以给我一个?我想你可以使用
hasRole([role])
方法(但我自己没有尝试过)
<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>
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();

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

auth = new UsernamePasswordAuthenticationToken(name, password);
<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>