【问题标题】:Multiple Role SecurityConfiguration Spring MVC + Thymeleaf多角色安全配置 Spring MVC + Thymeleaf
【发布时间】:2019-01-30 16:22:01
【问题描述】:

我有两种类型的用户角色,我喜欢在登录后为每种类型的用户设置一个不同的页面,但我不知道如何做到这一点。

安全配置

    @Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    public static final String SQL_LOGIN = "select username, password, active as enabled \n"
            + "from user where username = ?";

    public static final String SQL_PERMISSION = "select u.username, r.role as authority\r\n" + 
            "           from user u join user_role ur on u.id = ur.user_id join role r on ur.role_id = r.role_id\r\n" + 
            "           where u.username = ?";

    @Autowired
    private DataSource dataSource;

    @Autowired
    public void configurGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(SQL_LOGIN)
                .authoritiesByUsernameQuery(SQL_PERMISSION).passwordEncoder(passwordEncoder()); // bcrypt

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
        .antMatchers("/js/**").permitAll()
        .antMatchers("/css/**").permitAll()
        .antMatchers("/img/**").permitAll()
        .antMatchers("/fonts/**").permitAll()
        .antMatchers("/user/**").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login").permitAll()
        .defaultSuccessUrl("/vehicle/list", true)
        .and()
        .logout();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

WebMvcConfig

@Configuration

公共类 WebMvcConfig 实现 WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
}

如何创建不同的端点页面,一个用于 ADMIN,另一个用于用户?

//(ADMIN)
.loginPage("/login").permitAll() 
    .defaultSuccessUrl("/vehicle/list_admin", true)


//USER
.loginPage("/login").permitAll() 
        .defaultSuccessUrl("/vehicle/list", true)

类似的东西,有人可以帮帮我吗?

问候

【问题讨论】:

    标签: java spring security model-view-controller thymeleaf


    【解决方案1】:

    您需要实现一个 AuthenticationSuccessHandler 来检查角色并根据角色进行重定向。

    查看下一个答案,了解如何实现 Handler。

    AuthenticationSuccessHandler Spring Security

    【讨论】:

      【解决方案2】:

      您需要AuthenticationSuccessHandler。下面的代码应该可以解决问题。

      public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
          @Override
          public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws
                  IOException,
                  ServletException {
              User principal = (User) authentication.getPrincipal();
              boolean isAdmin = false;
              Iterator<GrantedAuthority> grantedAuthorityIterator = principal.getAuthorities().iterator();
              while (grantedAuthorityIterator.hasNext()) {
                  if (grantedAuthorityIterator.next().getAuthority().equalsIgnoreCase("ADMIN")) {
                      isAdmin = true;
                  }
              }
              if (isAdmin) {
                  response.sendRedirect("/vehicle/list_admin");
              } else {
                  response.sendRedirect("/vehicle/list");
              }
          }
      }
      

      此外,在您的 Spring Security 配置文件中,您将需要添加此选项。 .successHandler(CustomAuthenticationSuccessHandler).

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 2018-09-24
        • 1970-01-01
        • 1970-01-01
        • 2014-07-11
        • 2016-07-28
        • 2018-01-04
        相关资源
        最近更新 更多