簡體   English   中英

Spring Security(Java Config)登錄-根據用戶角色返回不同的URL

[英]Spring Security (Java Config) Login - Return Different URL Based on User Role

當用戶根據其角色登錄系統時,我想返回不同的頁面。

我有這種登錄方法,但是我不知道如何返回不同的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,則可能嗎?

非常感謝你!

您可以在.formLogin().logout函數上使用defaultSuccessUrlfailureUrl 這是一個例子:

.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM