当前位置: 首页 > 知识库问答 >
问题:

Spring Boot不显示web内容

方宏才
2023-03-14
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
o.s.security.web.FilterChainProxy        : /greeting at position 1 of 7 in additional filter chain; firing Filter: 'HeaderWriterFilter'
o.s.security.web.FilterChainProxy        : /greeting at position 2 of 7 in additional filter chain; firing Filter: 'StatelessLoginFilter'
o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/greeting'; against '/api/login'
o.s.security.web.FilterChainProxy        : /greeting at position 3 of 7 in additional filter chain; firing Filter: 'StatelessAuthenticationFilter'
o.s.security.web.FilterChainProxy        : /greeting at position 4 of 7 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
o.s.security.web.FilterChainProxy        : /greeting at position 5 of 7 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
o.s.s.w.a.AnonymousAuthenticationFilter  : Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
o.s.security.web.FilterChainProxy        : /greeting at position 6 of 7 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
o.s.security.web.FilterChainProxy        : /greeting at position 7 of 7 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/greeting'; against '/'
o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/greeting'; against '/documentation'
o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/greeting'; against '/greeting'
o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /greeting; Attributes: [permitAll]
o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@58e65a6f, returned: 1
o.s.s.w.a.i.FilterSecurityInterceptor    : Authorization successful
o.s.s.w.a.i.FilterSecurityInterceptor    : RunAsManager did not change Authentication object
o.s.security.web.FilterChainProxy        : /greeting reached end of additional filter chain; proceeding with original chain
o.s.s.w.a.ExceptionTranslationFilter     : Chain processed normally

我将greeting.html文件放在src/main/webapp/web-inf/templates和src/main/resources/templates中,我试图在application.properties中指定

# For the standard MVC JSTL view resolver
spring.view.prefix=/WEB-INF/templates/
spring.view.suffix=.html

我尝试了这些StackOverflow中提出的解决方案:“Spring Boot不提供静态内容”和“Spring Boot不启动静态web内容”,但没有任何改变...

最后是WebSecurityConfigurerAdapter:

public class StatelessAuthenticationSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private TokenAuthenticationService tokenAuthenticationService;

@Autowired
private LDAPAuthenticationService ldapAuthenticationService;

@Value("${ldap.useLdapForAuthentication}")
private String useLdapForAuthentication;

public StatelessAuthenticationSecurityConfig() {
    super(true);

}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .exceptionHandling().and()
            .anonymous().and()
            .servletApi().and()
            .headers().cacheControl().and()
            .authorizeRequests()

            //allow anonymous resource requests
            .antMatchers("/").permitAll()
            .antMatchers("/documentation").permitAll()
            .antMatchers("/greeting").permitAll()

            .antMatchers("/favicon.ico").permitAll()
            .antMatchers("/resources/**").permitAll()

            //allow anonymous POSTs to login
            .antMatchers(HttpMethod.OPTIONS, "/api/login").permitAll()
            .antMatchers(HttpMethod.POST, "/api/login").permitAll()
            .antMatchers(HttpMethod.OPTIONS, "/api/**").permitAll()
            .antMatchers(HttpMethod.POST, "/api/**").hasAnyRole("ADMIN", "USER")
            .antMatchers(HttpMethod.GET, "/api/**").hasAnyRole("ADMIN", "USER") //e compagnia cantando 

            //defined Admin only API area
            .antMatchers("/admin/**").hasRole("ADMIN")

            //all other request need to be authenticated
            .anyRequest().hasRole("USER")
            .and()              

            // custom JSON based authentication by POST of {"username":"<name>","password":"<password>"} which sets the token header upon authentication
            .addFilterBefore(new StatelessLoginFilter("/api/login", tokenAuthenticationService, userDetailsService, ldapAuthenticationService, authenticationManager(), useLdapForAuthentication), UsernamePasswordAuthenticationFilter.class)

            // custom Token based authentication based on the header previously given to the client
            .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class);
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}

@Override
protected UserDetailsService userDetailsService() {
    return userDetailsService;
}
@RestController
public class GreetingController {

@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
    model.addAttribute("name", name);
    return "greeting";
}
}

共有1个答案

邓驰
2023-03-14

根据Spring指南:构建RESTful Web服务

传统MVC控制器和上面的RESTful web服务控制器之间的一个关键区别是创建HTTP响应体的方式。这个RESTful web服务控制器不依赖视图技术来执行将问候语数据呈现到HTML的服务器端,而是简单地填充并返回一个问候语对象。对象数据将作为JSON直接写入HTTP响应。

因此,在您的示例中,它在JSON中返回“Greetines”。如果要让它返回页面greeting.html,则应该使用普通的@controller

 类似资料:
  • 我是新来的。使用Liferay 6.2。我做了一个自定义页面模板(),用户组D的所有用户默认采用页面模板。在这个页面模板中我添加了portlet,带有和。 我的问题是,当一个用户在这个web内容portlet中添加数据时,所有其他用户都会在他们的页面中看到相同的数据。 我需要改变这个,每个用户都能看到他们的数据。 怎么能这样?有人能帮我吗? 非常感谢。

  • 我试图在TableView中插入一些值,但它不显示列中的文本,尽管这些列不是空的,因为有三行(与我在TableView中插入的条目数量相同)可单击。 我有一个类“ClientIController”,它是fxml文件的控制器,这些是TableView和TableView的列的声明。 我在initialize()方法中调用了一个名为loadTable()的函数,该函数将内容添加到TableView中

  • 我在运行webpack dev服务器时遇到以下问题:当我运行npm start时,它显示以下内容: ➜ 目录git:(暂存)✗ npm启动 目录 @1.0.0 启动目录 BUILD_DEV=1 BUILD_STAGING=1 ./node_modules/webpack-dev-server/bin/webpack-dev-server.js http://localhost:8080/ webp

  • 我使用fiddler监控一个简单的html内容从一个PHP文件运行在localhost。但是每当我按f5刷新页面(浏览器)时,在fiddler中有时整个web会话的字体变成蓝色,即当它实际显示内容(html)时,相反的情况发生在web会话是灰色的时候,它不显示html内容。 注意:始终显示请求/响应标题,这仅用于内容。我还尝试了点技巧(“:80”)并从localhost切换到127.0。0.1.

  • 我已经为我的spring boot应用程序配置了测微计和prometheus,我可以在endpoint/执行器/prometheus处看到以下指标(使用计时器生成): 但当我在Grafana中运行其中一个查询(针对prometheus实例配置)时,我没有看到任何结果。 这需要任何配置吗?

  • 我正在尝试为我的JavaWeb应用程序项目创建一个描述符文件。对于这个项目,我正在使用: 当我尝试创建glassfish描述符文件时。Netbean显示到对话框窗口中,该窗口将创建一个名为sun-web.xml的文件,而不是glassfish-web.xml. 我在互联网上寻找这个,发现第一个sun-web.xml是为早于3的版本创建的,版本4应该创建一个名为glassfish-web.xml的文