久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 黄色男女网站 | 淫片一级国产 | 欧美日韩国产一区二区 | 久久精品免费观看 | 国产剧情一区二区三区 | 国产最新视频在线 | 在线免费亚洲视频 | 久久国产精品久久国产精品 | 精品国产乱码久久久久久影片 | 超碰免费在线观看 | 免费日本视频 | 欧美一级久久精品 | 国产一区二区三区免费观看视频 | 亚洲精品日韩欧美 | 久久伊人一区 | 久久成人亚洲 | 国产福利视频网站 | 午夜久久久久久久久久一区二区 | 范冰冰一级做a爰片久久毛片 | 蜜桃精品视频在线 | 中文成人无字幕乱码精品 | 日韩一区二区在线播放 | 91精品国产91久久久久久三级 | 狠狠伊人 | 免费观看一级特黄欧美大片 | 欧美在线一区二区三区 | 天堂久| av免费入口 | 久久在线| 中文字幕视频一区二区 | 免费激情网站 | 国产欧美日韩一区 | 91精品国产综合久久久久久 | 久久天堂网| 亚洲成人毛片 | 欧美日韩精品一区二区三区四区 | 精品一区二区免费视频 | 亚洲一区二区三区在线视频 | 亚洲国产精品一区二区第一页 | 天天操操 | 欧美成人精品 |