[英]Spring security How redirect after succes login page with user role?
[英]spring security redirect user by ROLE
我想根據他的角色將用戶重定向到一個頁面,但我不知道該怎么做。 我到處搜索,但它對我不起作用。 即使用戶有 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");
}
}
任何想法我該怎么做?
實現您自己的AuthenticationSuccessHandler
從authentication
對象檢查用戶角色並將重定向發送到合適的 URL
<bean id="authenticationSuccessHandler" class="..." />
<form-login authentication-success-handler-ref="authenticationSuccessHandler" ... />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.