Spring 安全(Java 配置)登录 - Return 不同 URL 基于用户角色
Spring Security (Java Config) Login - Return Different URL Based on User Role
我想在用户根据角色登录系统时return不同的页面。
我有这种登录方法,但我不知道 return 与 URL 有何不同。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**", "/data/**", "/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
我该怎么做?如果你不添加一个新的控制器,在登录后重定向并且这个重定向到不同的 url in base him role 是可能的吗?
非常感谢!
您可以在 .formLogin()
和 .logout
函数上使用 defaultSuccessUrl and failureUrl。这是一个例子:
.formLogin()
.loginPage("/login").loginProcessingUrl("/login/validate")
.defaultSuccessUrl("/").failureUrl("/login?error=true")
.usernameParameter("username").passwordParameter("password")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout=true")
就每个用户角色的重定向而言,我建议 defaultSuccessUrl
页面具有基于用户角色的重定向:
<sec:authorize access="hasRole('ROLE_ADMIN')">"
<c:redirect url="/admin.html"/>
</sec:authorize>
<sec:authorize access="hasRole('ROLE_USER')">"
<c:redirect url="/user.html"/>
</sec:authorize>
我想在用户根据角色登录系统时return不同的页面。
我有这种登录方法,但我不知道 return 与 URL 有何不同。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**", "/data/**", "/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
我该怎么做?如果你不添加一个新的控制器,在登录后重定向并且这个重定向到不同的 url in base him role 是可能的吗?
非常感谢!
您可以在 .formLogin()
和 .logout
函数上使用 defaultSuccessUrl and failureUrl。这是一个例子:
.formLogin()
.loginPage("/login").loginProcessingUrl("/login/validate")
.defaultSuccessUrl("/").failureUrl("/login?error=true")
.usernameParameter("username").passwordParameter("password")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout=true")
就每个用户角色的重定向而言,我建议 defaultSuccessUrl
页面具有基于用户角色的重定向:
<sec:authorize access="hasRole('ROLE_ADMIN')">"
<c:redirect url="/admin.html"/>
</sec:authorize>
<sec:authorize access="hasRole('ROLE_USER')">"
<c:redirect url="/user.html"/>
</sec:authorize>