我正在尝试在我的项目中实现JWTAuthentication。我的实体设置如下:
@Entity
public class ApplicationUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
String username,password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后我像这样设置了AuthenticationFilter:
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private AuthenticationManager authenticationManager;
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
try {
System.out.println("request "+request.getPathInfo());
ApplicationUser user = new ObjectMapper().readValue(request.getInputStream(),ApplicationUser.class);
//this is where the exception is
return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(),user.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(((ApplicationUser) authResult.getPrincipal()).getUsername())
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
.signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
.compact();
response.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
}
}
这是我的安全配置:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private BCryptPasswordEncoder bCryptPasswordEncoder;
private UserDetailsService userDetailsService;
public SecurityConfig(AppUserDetailService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder){
this.bCryptPasswordEncoder=bCryptPasswordEncoder;
this.userDetailsService=userDetailsService;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
// .antMatchers(HttpMethod.POST,LOGIN_URL).permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
.addFilter(new JWTAuthorizationFilter(authenticationManager()))
// this disables session creation on Spring Security
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
注册工作正常,但当我尝试使用相同的凭据登录时,会出现以下异常:
由: com.fasterxml.jackson.databind.exc.MismatchedInputException: 由于输入结束,没有要映射的内容 [来源: (org.apache.catalina.connector.CoyoteInputStream); 行: 1, 列: 0] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4145) ~[jackson-databind-2.9.6.jar:2.9.6] atcom.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000) ~[jackson-databind-2.9.6.jar:2.9.6] at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3070) ~[jackson-databind-2.9.6.jar:2.9.6] at com.project.pq.security.JWTAuthenticationFilter.tryAuthentication(JWTAuthenticationFilter.java:35) ~[classes/:na] ...省略了 53 个常见帧
我使用Postman访问API,在那里我将application/json设置为内容类型,并将用户名和密码作为post参数发送。我正在学习这个教程。我做错了什么?任何帮助都将不胜感激。
谢谢。
我有同样的错误,不得不删除应用程序用户用户=新的ObjectMapper().readValue(request.getInputStream(),ApplicationUser.class);
并直接使用request.getParameter:
new UsernamePasswordAuthenticationToken(
request.getParameter("username"),
request.getParameter("password"),
Collections.emptyList()
)
我从服务器收到此响应 我试图使用Jackson解析器库解析这个json字符串,但是不知何故,我遇到了映射异常声明 为什么我们会有这种例外? 如何理解是什么导致了这个异常? 我正在尝试使用以下方式解析: 状态响应类
我正在尝试转换下一个字符串: 对于某些自定义对象: 此外,此CustomObject中还有另一个对象: 现在,我正在尝试做以下工作: 但我得到了下一个错误:
考虑以下方法:
考虑以下方法: 当我从Postman调用WS时,我得到以下异常: 08-Feb-2019 14:23:44.138 GRAVE [http-nio-8080-exec-7] com.sun.jersey.spi.container.ContainerResponse.mapMappableContainerException MappableContainerException MappableC
问题内容: 我正在针对REST控制器POST处理程序进行集成测试。好吧,我正在努力。 它给我HttpMessageNotReadableException异常:无法读取JSON:由于输入结束,没有内容要映射 这是我的控制器: 基本测试类: 集成测试: 控制台日志必须说些什么: 有什么线索吗? 问题答案: 我将.param()方法替换为.content()方法之一: 现在,它可以按预期工作。
(更新的代码)无论出于什么原因,InputMismatchException的catch块无法正常工作。当代码抛出此错误时,catch块不会捕获它。有人知道为什么会这样吗?