簡體   English   中英

Spring Boot編碼過濾器

[英]Spring boot encoding filter

我使用Spring Boot和Spring Security,並且需要對請求進行編碼。 因此,我在Spring Security中使用了編碼過濾器,並將其添加到其他過濾器之前。 而且它不起作用。 我得到以下結果:

在此處輸入圖片說明

@Configuration
@EnableWebSecurity
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyUserDetailsService myUserDetailsService;

@Autowired
private UserDao userDao;

@Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler;

@Autowired
private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

@Bean(name = "myAuthenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Bean
public PasswordEncoder customPasswordEncoder() {
    return new PasswordEncoder() {
        @Override
        public String encode(CharSequence rawPassword) {
            return DigestUtils.md5Hex(rawPassword.toString());
        }
        @Override
        public boolean matches(CharSequence rawPassword, String encodedPassword) {
            return DigestUtils.md5Hex(rawPassword.toString()).equals(encodedPassword);
        }
    };
}

@Bean
public CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter()
        throws Exception {
    CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new CustomUsernamePasswordAuthenticationFilter(userDao);
    customUsernamePasswordAuthenticationFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login", "POST"));
    customUsernamePasswordAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
    customUsernamePasswordAuthenticationFilter.setAuthenticationFailureHandler(customAuthenticationFailureHandler);
    customUsernamePasswordAuthenticationFilter
            .setAuthenticationManager(authenticationManagerBean());
    return customUsernamePasswordAuthenticationFilter;
}

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


@Override
protected void configure(HttpSecurity http) throws Exception {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    http.addFilterBefore(filter,CsrfFilter.class);
    //Implementing Token based authentication in this filter
    final TokenAuthenticationFilter tokenFilter = new TokenAuthenticationFilter(userDao);
    http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);
    http.addFilterBefore(customUsernamePasswordAuthenticationFilter(), CustomUsernamePasswordAuthenticationFilter.class);

    http.csrf().disable();
    http.cors();

    http.sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http.exceptionHandling()
            .authenticationEntryPoint(restAuthenticationEntryPoint)
            .and()
            .authorizeRequests()
            .antMatchers("/news/create").hasAnyAuthority("ADMIN")
            .antMatchers("/news/update").hasAnyAuthority("ADMIN")
            .antMatchers("/news/update/*").hasAnyAuthority("ADMIN")
            .antMatchers("/news/delete").hasAnyAuthority("ADMIN")
            .antMatchers("/**").hasAnyAuthority("USER", "ADMIN")
            .and()
            .logout();
}

//cors configuration bean
...
}

我使用了許多不同的方法來解決它。 但是什么都行不通...我現在無法發布此問題,因為有很多代碼。 非常抱歉,但是我必須寫一些句子才能發布它)謝謝

嘗試在application.properties添加編碼配置

如下 :

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

請參閱文檔以獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM