Spring Boot 集成登陸模塊

背景

項(xiàng)目中前后端通信時(shí),有需求權(quán)限認(rèn)證的步驟,這里使用SpringSecurtiy + Jwt 采用token驗(yàn)證的方式完成登錄功能。

步驟

  1. 添加配置類
/**
 * Created by Justin on 2017/6/2.
 */

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    // 指定加密方式
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilter() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                // 設(shè)置UserDetailsService
                .userDetailsService(this.userDetailsService)
                // 設(shè)置passwordEncoder
                .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 由于使用的是JWT,我們這里不需要csrf
                .csrf().disable()

                // 基于token,所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()

                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
//                .antMatchers("/**").permitAll()       // FOR TEST

                // 允許對(duì)于網(wǎng)站靜態(tài)資源的無授權(quán)訪問
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/*.html",
                        "/favicon.ico",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/bower_components/**",
                        "/file/**",
                        "/styles/**"
                ).permitAll()

                // 對(duì)于獲取token的rest api要允許匿名訪問
                .antMatchers("/auth/**").permitAll()

                // 除上面外的所有請(qǐng)求全部需要鑒權(quán)認(rèn)證
                .anyRequest().authenticated();

        // 禁止緩存
        httpSecurity.headers().cacheControl();

        // 添加JWT filter
        httpSecurity
                .addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}
  1. 添加配置(build.gradle)
# Token
token.tokenHeader=Authorization
token.tokenHead=Bearer 
token.secret=mySecret
token.initRole=user
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,789評(píng)論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 中國是一個(gè)注重家庭和傳承的國家,每一個(gè)父母都悉心栽培自己的子女,抱著望女成鳳望子成龍的期望為自己的子女付出了所有。...
    曉陽愛讀書閱讀 1,284評(píng)論 0 0
  • 走一個(gè)人的路 看獨(dú)自的風(fēng)景 不必分享太多 也不必去找理由 無差自在的生活多好 何必要強(qiáng)迫在一起 即使唱同一首歌也會(huì)...
    半片世界閱讀 186評(píng)論 0 0

友情鏈接更多精彩內(nèi)容