【问题标题】:spring security redirect user by ROLEROLE 的 spring 安全重定向用户
【发布时间】:2015-07-05 18:21:36
【问题描述】:

我想根据用户的角色将用户重定向到一个页面,但我不知道该怎么做。 我到处搜索,但它对我不起作用。即使用户有 ROLE_USER 或其他,它也会为 ROLE_MASTER 重定向。 正如您在注释代码中看到的那样,我尝试了不同的方式,但它们都不能正常工作。

代码是:

    @Configuration
    @EnableWebSecurity
    public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UsersDetailsServiceImpl usersDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("stefan").password("1234").roles("ADMIN");
        auth.userDetailsService(usersDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/res/**");
    }

    //.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http    .authorizeRequests()
                .antMatchers("/master/**").access("hasRole('ROLE_MASTER')")
                .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
                .antMatchers("/user/**").access("hasRole('ROLE_USER')")
                .antMatchers("/viewer/**").access("hasRole('ROLE_VIEWER')")
                .and()
                .formLogin().loginPage("/").failureUrl("/?error")
                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/master")
                .and()
                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
                .and()
                .csrf();

//                .and()
//                .authorizeRequests()
//                .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/admin")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf()
//
//                .and()
//                .authorizeRequests()
//                .antMatchers("/user/**").access("hasRole('ROLE_USER')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/user")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf()
//
//                .and()
//                .authorizeRequests()
//                .antMatchers("/viewer/**").access("hasRole('ROLE_VIEWER')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/viewer")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf();


//        http.authorizeRequests()
//                .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/admin")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf();

//        http.authorizeRequests()
//                .antMatchers("/user/**").access("hasRole('ROLE_USER')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/user")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf();

//        http.authorizeRequests()
//                .antMatchers("/viewer/**").access("hasRole('ROLE_VIEWER')")
//                .and()
//                .formLogin().loginPage("/").failureUrl("/?error")
//                .usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/viewer")
//                .and()
//                .logout().logoutUrl("/logout").logoutSuccessUrl("/?logout")
//                .and()
//                .csrf();
        //  http.formLogin().loginPage("/admin/login").failureUrl("/admin/login?error").defaultSuccessUrl("/main",true).usernameParameter("username").passwordParameter("password");

    }

}

任何想法我该怎么做?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    实现您自己的 AuthenticationSuccessHandlerauthentication 对象检查用户角色并将重定向发送到合适的URL

    <bean id="authenticationSuccessHandler" class="..." />
    <form-login authentication-success-handler-ref="authenticationSuccessHandler" ... />
    

    full example with roles

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 2016-10-26
      • 2018-11-17
      • 2014-03-26
      • 2018-05-19
      • 2014-11-11
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多