java - 登录认证后spring boot jwt没有重定向
问题描述
在更正我的代码时需要帮助,登录身份验证后它没有重定向到主页
security config.java
@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class Security_Config extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationSuccessHandler successHandler;
@Autowired
private JwtFilter jwtFilter;
@Autowired
private CustomUserDetailService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("http="+http);
// We don't need CSRF for this example
// http.csrf().disable()
// // dont authenticate this particular request
// .authorizeRequests().antMatchers("/authenticate").permitAll().
// antMatchers("/Login.html").permitAll().
// // all other requests need to be authenticated
// anyRequest().authenticated().and().
// // make sure we use stateless session; session won't be used to
// // store user's state.
// exceptionHandling().and().sessionManagement()
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/authenticate","/Login.html").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/Login").permitAll()
.loginProcessingUrl("/Login").permitAll()
.defaultSuccessUrl("/Home", true).permitAll()
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/Logout")).permitAll()
.clearAuthentication(true)
.logoutSuccessUrl("/Login").permitAll()
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true).and()
.exceptionHandling()
.accessDeniedPage("/Login")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// // If a user try to access a resource without having enough permissions
// http.exceptionHandling().accessDeniedPage("/Login.html");
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
}
}
解决方案
在我看来,JWT 身份验证的处理方式与传统的基于表单的身份验证不同,而在身份验证之后,我们应该返回 JWTTocken 而不是重定向到另一个页面。
推荐阅读
- angularjs - 是否有 angularjs 形式的属性来仅获取更改的值?
- c# - EF Core MySQL - LINQ 语句创建低效查询
- web-scraping - 使用 html 标签从谷歌搜索页面抓取网页
- html - 1024px 容器内的 100vw 图像导致小水平滚动
- javascript - 如果有二维坐标,如何获取一维数组的信息?
- node.js - socket.io 和异步查询有问题
- authentication - 用于日历读取报告的图形 API 访问令牌 无权限
- azure-data-factory - Azure 数据工厂使用 Databricks 中的现有群集
- java - 如何有效地在数组中随机重新插入元素
- date - 将日期压缩为唯一的字母数字字符