安全配置,主要继承 WebSecurityConfigurerAdapter 实现访问资源之前的拦截配置。该拦截器的顺序在资源服务器拦截器之前。

新建类,例 WebSecurityConfigurer.java,编写 @Configuration 类继承 WebSecurityConfigurerAdapter。使用的是 WebSecurityConfigurerAdapter 中默认的 HttpSecurity 对象的配置,该配置是要求应用中所有 URL 的访问都需要进行验证。我们也可以自定义哪些 URL 需要权限验证,哪些不需要。只需要在我们的 WebSecurityConfigurer 类中覆写 configure(HttpSecurity http) 方法即可。

重写(@Override) void configure(HttpSecurity http) 方法。

    /**
     * 拦截配置
     *
     * @param http http
     * @throws Exception Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .antMatchers("/oauth/token").permitAll()
                .and()
                .csrf().disable();
    }

除了需要重写 void configure 外,还需要 重写实现 configure(AuthenticationManagerBuilder auth)

    /**
     *
     * @param auth auth
     * @throws Exception Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

results matching ""

    No results matching ""