我正在设置服务器,其中大多数是rest控制器,并且它们工作正常。但是,当我创建一个新的普通控制器时,它只是简单地返回到html(显示html),但返回的状态找不到404。
如果网络安全类没有authenticationManagerBean和jwt过滤器,它将正常工作
WebSecurityConfig:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@Autowired
private JwtAuthEntryPoint unauthorizedHandler;
@Bean
public JwtAuthTokenFilter authenticationJwtTokenFilter() {
return new JwtAuthTokenFilter();
}
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/api/web/**").permitAll()
.antMatchers( "/favicon.ico").permitAll()
.and().authorizeRequests()
.anyRequest().authenticated().and() .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
WebController:
@Controller
@RequestMapping("/api/web")
public class WebController {
@GetMapping(value = "/welcome")
public String welcomePage(Model model) {
return "403Page";
}
}
HTML:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Access Denied</title>
</head>
<body>
<h3>403</h3>
</body>
</html>
结构:(我试图更改html的位置,但是无效)
main
| - java
| | - config
| | | - WebSecurityConfig.java
| | - controller
| | | - WebController.java
| - resources
| | - templates
| | | - 403Page.html
| | - static
| | | - 403Page.html
| | - WEB-INF
| | | - resources
| | | | - 403Page.html
| - webapp
| | - WEB-INF
| | | - 403Page.html
它可以直接访问url路径(api / web / welcome,我放入System.out.print()进行检查),但无法为我返回html显示内容