亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Security 的 permitAll Unauthorized

Spring Security 的 permitAll Unauthorized

慕碼人8056858 2021-10-28 09:44:46
我想忽略所有帶有HttpMethod.GET的 URL,帶有 Post、Delete、Put 的 URL 需要進行身份驗證。我的網址是"/api/manga","/api/grupos","/api/autor","/genero","/api/pagina","/api/capitulo"PermitAll 不適用于 JWTFilter,如果刪除過濾器,則工作正常。如何忽略或允許所有網址HttpMethod.GET?需要創建單獨的 api 進行身份驗證?網絡安全配置@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests()                .antMatchers(HttpMethod.GET, "/api/manga", "/api/grupos", "/api/autor", "/genero", "/api/pagina",                        "/api/capitulo")                .permitAll().anyRequest().fullyAuthenticated().and()                .addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class).httpBasic()                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf()                .disable();    }    @Override    public void configure(WebSecurity web) throws Exception {        web.ignoring().antMatchers("/favicon.ico", "/", "/index.html", "/registrar", "/autenticar", "/app/**");    }}JWT認證過濾器public class JWTAuthenticationFilter extends GenericFilterBean {    private static final String AUTHORIZATION_HEADER = "Authorization";    private static final String AUTHORITIES_KEY = "roles";    @Override    public void doFilter(final ServletRequest req, final ServletResponse res,final FilterChain filterChain)            throws IOException, ServletException {              final HttpServletRequest request = (HttpServletRequest) req;               String authReader = request.getHeader(AUTHORIZATION_HEADER);        if (authReader == null || !authReader.startsWith("Bearer ")) {            ((HttpServletResponse) res).sendError(HttpServletResponse.SC_UNAUTHORIZED, "invalido autorization");        } 
查看完整描述

2 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

解決方案是 ignore HttpMethod.GET,因此所有帶有 get 方法的 url 都將被忽略。


    @Override

    public void configure(WebSecurity web) throws Exception {

        web.ignoring()

        .antMatchers(HttpMethod.GET)

        .antMatchers("/favicon.ico", "/", "/index.html", "/registrar",

                "/autenticar", "/app/**");

    }


查看完整回答
反對 回復 2021-10-28
  • 2 回答
  • 0 關注
  • 381 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號