【发布时间】: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