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

Spring Boot@Controller在独立部署Tomcat时不工作

刘弘济
2023-03-14

我有一个Spring Boot Application ThymeLeaf,带有@RestController和@Controller,嵌入式tomcat一切正常,但当我将带有war文件的应用程序部署到Tomcat 8.5@Controller类不工作@RestController工作正常,当我使用@Controller访问任何带有控制器的页面时,我有404错误。

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example.boot</groupId>
        <artifactId>boot-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>api</artifactId>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>com.example.boot</groupId>
            <artifactId>web</artifactId>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>[5.0.2,5.1.47)</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <scope>provided</scope>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <skip>false</skip>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
                    <warName>skillinventory</warName>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/resources/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.parent.basedir}/web-si/src/main/web/dist/FrontEnd/</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这是我的Spring Boot应用程序类:

@SpringBootApplication
@EnableScheduling
@PropertySource("classpath:application.properties")
public class Application extends SpringBootServletInitializer {

    @Value("${cas.service}")
    private String service;

    @Value("${cas.loginUrl}")
    private String loginUrl;

    @Value("${cas.cas30ServiceTicketValidator}")
    private String cas30ServiceTicketValidator;

    @Value("${cas.createAuthorityList}")
    private String createAuthorityList;

    @Value("${cas.key}")
    private String key;

    @Value("${cas.logoutFilterLink}")
    private String logoutFilterLink;

    @Value("${cas.setFilterProcessesUrl}")
    private String setFilterProcessesUrl;

    @Value("${cas.setCasServerUrlPrefix}")
    private String setCasServerUrlPrefix;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServiceProperties serviceProperties() {
        ServiceProperties serviceProperties = new ServiceProperties();
        serviceProperties.setService(service);
        serviceProperties.setSendRenew(false);
        return serviceProperties;
    }

    @Bean
    @Primary
    public AuthenticationEntryPoint authenticationEntryPoint(ServiceProperties sP) {
        CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
        entryPoint.setLoginUrl(loginUrl);
        entryPoint.setServiceProperties(sP);
        return entryPoint;
    }

    @Bean
    public TicketValidator ticketValidator() {
        return new Cas30ServiceTicketValidator(cas30ServiceTicketValidator);
    }

    @Bean
    public CasAuthenticationProvider casAuthenticationProvider() {
        CasAuthenticationProvider provider = new CasAuthenticationProvider();
        provider.setAuthenticationUserDetailsService(customUserDetailsService());
        provider.setKey(key);
        return provider;
    }
    @Bean
    public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> customUserDetailsService() {
        return token -> {
            AttributePrincipal principal = token.getAssertion().getPrincipal();
            String name = principal.getName();
            return new User(name, "pwd", AuthorityUtils.createAuthorityList(createAuthorityList));
        };
    }

    @Bean
    public SecurityContextLogoutHandler securityContextLogoutHandler() {
        return new SecurityContextLogoutHandler();
    }

    @Bean
    public LogoutFilter logoutFilter() {
        LogoutFilter logoutFilter = new LogoutFilter(logoutFilterLink, securityContextLogoutHandler());
        logoutFilter.setFilterProcessesUrl(setFilterProcessesUrl);
        return logoutFilter;
    }

    @Bean
    public SingleSignOutFilter singleSignOutFilter() {
        SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
        singleSignOutFilter.setCasServerUrlPrefix(setCasServerUrlPrefix);
        singleSignOutFilter.setIgnoreInitConfiguration(true);
        return singleSignOutFilter;
    }

    @EventListener
    public SingleSignOutHttpSessionListener singleSignOutHttpSessionListener(HttpSessionEvent event) {
        return new SingleSignOutHttpSessionListener();
    }
}

我的安全配置

@EnableWebSecurity
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    private AuthenticationProvider authenticationProvider;
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    public SpringSecurityConfig(CasAuthenticationProvider
                                        casAuthenticationProvider,
                                AuthenticationEntryPoint authenticationEntryPoint) {
        this.authenticationProvider = casAuthenticationProvider;
        this.authenticationEntryPoint = authenticationEntryPoint;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/v1/datas/**")
                .permitAll()
                .and()
                .authorizeRequests()
                .regexMatchers("/")
                .authenticated()
                .and()
                .authorizeRequests()
                .regexMatchers("/")
                .permitAll()
                .and()
                .httpBasic()
                .authenticationEntryPoint(authenticationEntryPoint);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws
            Exception {
        auth.authenticationProvider(authenticationProvider);
    }


    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return new ProviderManager(Arrays.asList(authenticationProvider));
    }

    @Bean
    public CasAuthenticationFilter casAuthenticationFilter(ServiceProperties serviceProperties) throws Exception {
        CasAuthenticationFilter filter = new CasAuthenticationFilter();
        filter.setServiceProperties(serviceProperties);
        filter.setAuthenticationManager(authenticationManager());
        return filter;
    }

带@控制器的我的控制器:

@Controller
public class ManagerDataController {

    @Value("${theme.root}")
    private String themeRoot;

    @RequestMapping(value = "/hr/skilldatas", method = RequestMethod.GET)
    public String goProgessData(Model model) {
        model.addAttribute("themeRoot", themeRoot);
        return "data-process";
    }

  @RequestMapping(value = "/hr/datas", method = RequestMethod.GET)
  public String goDataManager(Model model) {
      model.addAttribute("themeRoot", themeRoot);
      return "datamanager";
  }

}

我不知道为什么它与tomcat嵌入式工作正常,但tomcat独立?

共有1个答案

姜博
2023-03-14

原来,因为我声明thymeleaf依赖范围是提供的,它不是Spring false,在我更改范围后编译它现在工作。

 类似资料:
  • 本文向大家介绍浅谈springboot内置tomcat和外部独立部署tomcat的区别,包括了浅谈springboot内置tomcat和外部独立部署tomcat的区别的使用技巧和注意事项,需要的朋友参考一下 前两天,我去面了个试,面试官问了我个问题,独立部署的tomcat跟springboot内置的tomcat有什么区别,为什么存在要禁掉springboot的tomcat然后将项目部署到独立的to

  • 我试图在liferay tomcat版本中部署一个简单的独立war文件。它既不是portlet,也不是主题。如何在liferay tomcat中将war文件配置为独立的war。 我有以下错误 16:45:51,175错误[localhost-1][HotDeployImpl: 211]com.liferay.portal.kernel.deploy.hot.HotDeployExc0019:错误注

  • 独立部署即为在后端运行程序,让程序跑在后台。 linux 在 linux 下面部署,我们可以利用 nohup 命令,把应用部署在后端,如下所示: nohup ./beepkg & 这样你的应用就跑在了 Linux 系统的守护进程 Windows 在 Windows 系统中,设置开机自动,后台运行,有如下几种方式: 制作 bat 文件,放在“启动”里面 制作成服务

  • 我试图详细学习JavaEE7,但我在从数据库中获取记录并在JSF页面上显示它们方面遇到了问题。 我使用Wildfly10.1.0和Oracle XE11。我创建了以下数据源: 在JBoss的管理接口中的连接测试是成功的。 这是我的: 当我通过运行WildFly,并通过部署我的应用程序时,它可以工作。 当我在Eclipse中启动服务器并尝试使用相同的命令部署应用程序时,它失败了--因为我添加了JPA

  • 我实现了一个Scalatra servlet,现在想要创建一个可执行的jar,就像本教程中所描述的那样:http://www.scalatra.org/2.2/guides/deployment/standalone.html 我使用IntelliJ IDEA和Scala插件进行开发,并使用sbt来构建和运行我的servlet(我使用sbt-想法来生成项目文件)。我的问题是,当我尝试编译我的项目时