I’m encountering an issue where a 500 error is displayed on the web when I try to design a feature for my app related to “taco” functionality. I suspect the issue might be related to converting a string to a class on the frontend. This feature is important for security, but I’m not sure how to resolve the error.Has anyone experienced something similar or can provide guidance on how to fix this?
When i was learning spring boot with the book “spring inaction sixth edition”, i face a proble in ch05. The book use the springboot 2.5.3 , when i use 3.3.1. i change the security config like this:
package org.mytaco.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
// 配置权限
http.authorizeHttpRequests((auth)-> auth
.requestMatchers("/design","/orders").authenticated()
.requestMatchers("/","/**").permitAll()
);
http.formLogin((login)-> login
.loginPage("/login"));
http.logout((logout)-> logout
.logoutSuccessUrl("/"));
// 用于h2-console调试
http.csrf((csrf) -> csrf
.ignoringRequestMatchers("/h2-console/**"));
http.headers((headers) -> headers
.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin));
return http.build();
}
@Bean
public PasswordEncoder encoder(){
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager
(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
it is useful in secutry spect .but when i design taco –one fuction of the app. a 500 code display on the web.
i think it might be An error occurred while converting the string to a class on the frontend
i create a convert like this:
package org.mytaco.web;
import org.mytaco.data.IngredientRepository;
import org.mytaco.domain.Ingredient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@Component
public class IngredientByIdConverter
implements Converter<String, Ingredient> {
private final IngredientRepository ingredientRepo;
@Autowired
public IngredientByIdConverter(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@Override
public Ingredient convert(String id) {
return ingredientRepo.findById(id).orElse(null);
}
}
but it still unable:
2024-08-18T15:47:32.347+08:00 ERROR 3004 --- [MyTaco] [nio-8080-exec-6] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-6] Exception processing template "orderForm": An error happened during template parsing (template: "class path resource [templates/orderForm.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/orderForm.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "order.tacos" (template: "orderForm" - line 24, col 13)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "order.tacos" (template: "orderForm" - line 24, col 13)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'tacos' cannot be found on null
2024-08-18T15:47:32.362+08:00 ERROR 3004 --- [MyTaco] [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/orderForm.html]")] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'tacos' cannot be found on null
zfmx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.