簡體   English   中英

使用 Spring Boot 了解身份驗證和會話管理

[英]Understanding authentication and session management using Spring boot

我用 mongo db 作為后端做了一個 spring boot 項目。 我想確保在維護某種內存會話的同時對用戶進行身份驗證和授權(使用 redis 或內置的 spring 會話)
我經歷過很多像turorials的這個這個這個

他們都要求您擴展WebSecurityConfigAdapter ,配置HttpSecurity並提供UserDetailService 我已經通過以下方式完成了。

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService(){
        return new StockUserDetailService();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login/**").permitAll()
                .antMatchers("/logout/**").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN")
                .antMatchers("/broker/**").hasAnyAuthority("BROKER")
                .anyRequest().fullyAuthenticated();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }
}

但我不明白的是 sessin 管理在哪里進行,用戶應該使用什么url登錄? 如果我編寫了一個帶有/login映射的控制器,那么我的操作應該在登錄控制器中做什么。 我真的沒有得到全貌。



更新

我嘗試發布到/login 我收到這個錯誤

{
"timestamp": 1494842451672,
"status": 403,
"error": "Forbidden",
"message": "Could not verify the provided CSRF token because your session was not found.",
"path": "/login"
}

Spring 為您管理/login路由。 您只需要使用用戶憑據向其發送 POST 請求。 如果您想指定另一個登錄 URL,您可以使用.loginProcessingUrl("/myCustomLoginUrl")對於UserDetailsService您必須提供您自己的實現來實現loadUserByUsername(String userName)方法,該方法從數據庫或您所在的任何持久性存儲中檢索用戶使用並返回具有相應權限的org.springframework.security.core.userdetails.User 請參閱以下官方文檔: https : //spring.io/guides/gs/securing-web/

暫無
暫無

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

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