黃鼠狼在養雞場山崖邊立了塊碑,寫道:「不勇敢地飛下去,你怎麼知道本身原來是一隻搏擊長空的鷹?!」java
今後之後git
黃鼠狼天天都能在崖底吃到那些摔死的雞!github
上週五有網友問道,在使用spring-security-oauth2
時,雖然配置了.antMatchers("/permitAll").permitAll()
,但若是在header
中 攜帶 Authorization Bearer xxxx
,OAuth2AuthenticationProcessingFilter
仍是會去校驗Token
的正確性,若是Token
合法,能夠正常訪問,不然,請求失敗。他的需求是當配置.permitAll()
時,即便攜帶Token
,也能夠直接訪問。spring
根據Spring Security源碼分析一:Spring Security認證過程得知spring-security
的認證爲一系列過濾器鏈。咱們只需定義一個比OAuth2AuthenticationProcessingFilter
更早的過濾器攔截指定請求,去除header
中的Authorization Bearer xxxx
便可。小程序
添加PermitAuthenticationFilter
類攔截指定請求,清空header
中的Authorization Bearer xxxx
微信小程序
@Component("permitAuthenticationFilter") @Slf4j public class PermitAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { log.info("當前訪問的地址:{}", request.getRequestURI()); if ("/permitAll".equals(request.getRequestURI())) { request = new HttpServletRequestWrapper(request) { private Set<String> headerNameSet; @Override public Enumeration<String> getHeaderNames() { if (headerNameSet == null) { // first time this method is called, cache the wrapped request's header names: headerNameSet = new HashSet<>(); Enumeration<String> wrappedHeaderNames = super.getHeaderNames(); while (wrappedHeaderNames.hasMoreElements()) { String headerName = wrappedHeaderNames.nextElement(); if (!"Authorization".equalsIgnoreCase(headerName)) { headerNameSet.add(headerName); } } } return Collections.enumeration(headerNameSet); } @Override public Enumeration<String> getHeaders(String name) { if ("Authorization".equalsIgnoreCase(name)) { return Collections.<String>emptyEnumeration(); } return super.getHeaders(name); } @Override public String getHeader(String name) { if ("Authorization".equalsIgnoreCase(name)) { return null; } return super.getHeader(name); } }; } filterChain.doFilter(request, response); } }
添加PermitAllSecurityConfig
配置用於配置PermitAuthenticationFilter
微信
@Component("permitAllSecurityConfig") public class PermitAllSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain,HttpSecurity> { @Autowired private Filter permitAuthenticationFilter; @Override public void configure(HttpSecurity http) throws Exception { http.addFilterBefore(permitAuthenticationFilter, OAuth2AuthenticationProcessingFilter.class); } }
@Override public void configure(HttpSecurity http) throws Exception { // @formatter:off http.formLogin() .successHandler(appLoginInSuccessHandler)//登陸成功處理器 .and() .apply(permitAllSecurityConfig) .and() .authorizeRequests() .antMatchers("/user").hasRole("USER") .antMatchers("/forbidden").hasRole("ADMIN") .antMatchers("/permitAll").permitAll() .anyRequest().authenticated().and() .csrf().disable(); // @formatter:ON }
添加permitAllWithTokenTest
方法數據結構
@Test public void permitAllWithTokenTest() throws Exception{ final String accessToken = obtainAccessToken(); log.info("access_token={}", accessToken); String content = mockMvc.perform(get("/permitAll").header("Authorization", "bearer " + accessToken+"11")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); log.info(content); }
Authorization bearer xxx 11
後面隨機跟了兩個參數🙂🙂🙂關注微信小程序java架構師歷程
上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提升本身的技術嗎?來吧這裏有你須要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?架構