Java Spring,Thymeleaf重定向到错误的控制器
问题是当我单击按钮(在homeHeader.html)时,它应该将我重定向到JobOfferController.java,然后再重定向到>viewJobslist(),但它没有发生,因为它将我重定向到登录页面 JobOfferController.javaJava Spring,Thymeleaf重定向到错误的控制器,java,spring-boot,spring-security,thymeleaf,Java,Spring Boot,Spring Security,Thymeleaf,问题是当我单击按钮(在homeHeader.html)时,它应该将我重定向到JobOfferController.java,然后再重定向到>viewJobslist(),但它没有发生,因为它将我重定向到登录页面 JobOfferController.java @Controller public class JobOfferController { @Autowired private JobOfferService jobOfferService; @RequestMapping(path
@Controller
public class JobOfferController {
@Autowired
private JobOfferService jobOfferService;
@RequestMapping(path ="/jobsList" , method = RequestMethod.GET)
public String viewJobsList(Model model){
List<JobOffer> jobOfferList = jobOfferService.lisAll();
model.addAttribute("jobOfferList", jobOfferList);
return "viewJobs.html";
}
}
@Controller
public class LoginController {
@Autowired
UserService userService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/all/**").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/register").permitAll()
.antMatchers("/user/**").hasAuthority("USER")
.antMatchers("/employee/**").hasAuthority("EMPLOYEE")
.antMatchers("/admin/**").hasAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.csrf().disable().formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.successHandler(sucessHandler)
.usernameParameter("email")
.passwordParameter("password")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.and()
.exceptionHandling()
.accessDeniedPage("/access-denied");
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**",
"/img/**","/webjars/**","/templates/**", "/font/**");
}
我还使用ResourceHandlerRegistry
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**", "/static/**", "/templates/**")
.addResourceLocations("/webjars/", "classpath:/static/", "classpath:/templates/");
}
我认为在JobOfferController.java中一切正常,但问题可能在于SpringSecurit,它不允许我重定向到JobOfferController
SecurityConfig.java的片段
@Controller
public class JobOfferController {
@Autowired
private JobOfferService jobOfferService;
@RequestMapping(path ="/jobsList" , method = RequestMethod.GET)
public String viewJobsList(Model model){
List<JobOffer> jobOfferList = jobOfferService.lisAll();
model.addAttribute("jobOfferList", jobOfferList);
return "viewJobs.html";
}
}
@Controller
public class LoginController {
@Autowired
UserService userService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/all/**").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/register").permitAll()
.antMatchers("/user/**").hasAuthority("USER")
.antMatchers("/employee/**").hasAuthority("EMPLOYEE")
.antMatchers("/admin/**").hasAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.csrf().disable().formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.successHandler(sucessHandler)
.usernameParameter("email")
.passwordParameter("password")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.and()
.exceptionHandling()
.accessDeniedPage("/access-denied");
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**",
"/img/**","/webjars/**","/templates/**", "/font/**");
}
您的(安全和rest)配置(如发布的)与您的(问题)描述一致:
/jobsList
(路径上下文)匹配(仅).anyRequest().authenticated()
(在您的安全配置中),因此被重定向到登录页面(/login
)…如果您想“打开”该页面:尝试@RequestMapping(path=“/all/jobsList”、…
(因为.antMatchers(“/all/**”).permitAll()
)或者:.antMatchers(“/jobsList”).permitAll()
添加到xerx59的注释中,您可以在控制器类级别使用RequestMapping在所有URI前面加上“/all”,这样到JobOfferController的每个请求映射都将始终是“/all”/**“其中**将是放入RequestMapping中的名称非常感谢您帮助了很多。您还可以简单地返回“viewJobs”
。并使用@GetMapping
,因为它略短。您的(安全和rest)配置(如发布的)与您的(问题)描述一致:/jobsList
(路径上下文)匹配(仅限).anyRequest().authenticated()
(在您的安全配置中),因此被重定向到登录页面(/login
)…如果您想“打开”该页面:尝试@RequestMapping(path=“/all/jobsList”,…
(因为.antMatchers(“/all/**”).permitAll()
)或只需:.antMatchers(/jobsList”).permitAll()
添加到xerx59的注释中,您可以使用控制器类级别的RequestMapping在所有URI前面加上“/all”,这样到JobOfferController的每个RequestMapping都将始终是“/all/**”,其中**将是放入RequestMapping的名称非常感谢您的帮助您还可以简单地返回“viewJobs”
。并使用@GetMapping
,因为它稍微短一点。