久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Spring Security UsernamePasswordAuthenticationFilter JWT 身

Set custom login url in Spring Security UsernamePasswordAuthenticationFilter JWT authentication(在 Spring Security UsernamePasswordAuthenticationFilter JWT 身份驗證中設置自定義登錄 url) - IT屋-程序員軟件開發技術分享社
本文介紹了在 Spring Security UsernamePasswordAuthenticationFilter JWT 身份驗證中設置自定義登錄 url的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在關注 此 auth0 的教程 以確保我的應用程序使用 JWT.

I'm following this auth0's tutorial to secure my application using JWT.

我最終得到了以下 WebSecurity 配置:

I've ended up with the following WebSecurity configuration:

@EnableWebSecurity
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class WebSecurity extends WebSecurityConfigurerAdapter {

    private final UserDetailsService userDetailsService;
    private final BCryptPasswordEncoder passwordEncoder;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .and().cors()
                .and().csrf()
                .disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, REGISTER_URL).permitAll()
                .antMatchers(HttpMethod.POST, LOGIN_URL).permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthorizationFilter(authenticationManager()))
                .addFilter(new JWTAuthenticationFilter(authenticationManager()))
                // This disables session creation on Spring Security
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

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

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }

}

以及以下 JWTAuthenticationFilter:

and the following JWTAuthenticationFilter:

public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    private final AuthenticationManager authenticationManager;

    public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        try {
            ApplicationUser credentials = new ObjectMapper().readValue(request.getInputStream(), ApplicationUser.class);
            return authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                            credentials.getUsername(),
                            credentials.getPassword(),
                            new ArrayList<>()
                    )
            );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
        String token = Jwts.builder()
                .setSubject(((User) authResult.getPrincipal()).getUsername())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
                .compact();
        response.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
    }
}

目前,該應用接受 /login URL 上的 POST 請求.我想知道如何將 URL 更改為,比如說,/api/auth/login.有沒有辦法將 URL 字符串注入身份驗證過濾器或以某種方式在安全配置中進行設置?

At the moment, the app accepts POST requests on the /login URL. I wonder how to change the URL to, let's say, /api/auth/login. Is there any way to inject the URL string into the authentication filter or to set it somehow in the security config?

推薦答案

你正在擴展 org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter 本身擴展org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.在這最后一個類中,有一個名為 setFilterProcessesUrl 的設置器,它的目的就是這樣做:

You are extending org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter which itself extends org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter. In this last class, there is a setter called setFilterProcessesUrl which is intended to do just this:

setFilterProcessesUrl

public void setFilterProcessesUrl(String filterProcessesUrl)

public void setFilterProcessesUrl(String filterProcessesUrl)

設置確定是否需要身份驗證的 URL

Sets the URL that determines if authentication is required

參數:filterProcessesUrl

Parameters: filterProcessesUrl

This 是指向該 javadoc 部分的鏈接

This is the link to that javadoc section

所以在你的 WebSecurityConfigurerAdapter 你可以這樣做:

So in your WebSecurityConfigurerAdapter you could do just like this:

@Bean
public JWTAuthenticationFilter getJWTAuthenticationFilter() {
    final JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager());
    filter.setFilterProcessesUrl("/api/auth/login");
    return filter;
}

然后在同一個類的 configure 方法中只引用它而不是創建新實例:

And then in your configure method in the same class just reference it instead of creating new instance:

.addFilter(getJWTAuthenticationFilter())

這篇關于在 Spring Security UsernamePasswordAuthenticationFilter JWT 身份驗證中設置自定義登錄 url的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: www日韩高清 | 久久久久久久一区二区 | 久久精品欧美一区二区三区麻豆 | 欧美激情a∨在线视频播放 成人免费共享视频 | a级大片免费观看 | 国产精品久久毛片av大全日韩 | 亚洲一区三区在线观看 | 网站黄色在线免费观看 | japanhdxxxx裸体 | 丝袜一区二区三区 | 国产一区二区免费 | 欧美久久久电影 | 成人免费观看视频 | 97精品一区二区 | www国产精品 | 男人的天堂久久 | 久久精品这里 | 欧美日韩a | 国产精品欧美一区二区 | 国产天天操 | 日本欧美在线视频 | 国产精品亚洲第一区在线暖暖韩国 | 成人精品一区二区三区中文字幕 | 日本电影免费完整观看 | 91免费看片神器 | 在线视频 欧美日韩 | 免费观看一级特黄欧美大片 | 伊人在线视频 | 三级视频在线观看 | 99re视频在线| 免费观看黄 | 日韩国产一区二区三区 | 日韩视频专区 | 中文字幕亚洲视频 | 欧美一级片久久 | 嫩草网| 色888www视频在线观看 | 99久久免费精品国产男女高不卡 | 欧美黄色小视频 | www.日本在线| 国产精品一区在线观看 |