2017-11-19 1 views
0

私はSpringを学んでいますが、私は解決できないいくつかの問題に立ち往生しています。 GlassFishサーバーで実行します。Spring 5 MVC - InternalResourceViewResolverとSpring Security

私はそれがこのようになり、InternalResourceViewResolverを持っているシンプルなWebMvcConfigurerを実装している:それは、

@WebServlet(value = "/") 
@Controller 
public class HomeController extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     request.getRequestDispatcher("index.jsp").forward(request,response); 
    } 
} 

私はアプリを実行すると仮定します:

@Configuration 
@EnableWebMvc 
public class WebMvcConfig implements WebMvcConfigurer { 

    @Bean 
    public InternalResourceViewResolver resolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setViewClass(JstlView.class); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

    @Bean 
    public MessageSource messageSource() { 
     ResourceBundleMessageSource source = new ResourceBundleMessageSource(); 
     source.setBasename("messages"); 
     return source; 
    } 

    @Override 
    public Validator getValidator() { 
     LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); 
     validator.setValidationMessageSource(messageSource()); 
     return validator; 
    } 
} 

と私のコントローラindex.jspファイルを開きますが、代わりに404が表示されます。

request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request,response); 

それを開きます。

また、私はSpring Securityに関するドキュメントを守り、簡単なセキュリティを実装しました。私の理解では、すべてのリクエストを認証し、ユーザーをログインページにリダイレクトする必要がありますが、そうではありません。ここで

は、コードは次のとおりです。

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Bean 
    public UserDetailsService userDetailsService() { 
     InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); 
     manager.createUser(User.withUsername("user").password("user").roles("USER").build()); 
     manager.createUser(User.withUsername("admin").password("admin").roles("USER", "ADMIN").build()); 
     return manager; 
    } 

    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .and() 
       .httpBasic(); 
    } 
} 

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected String[] getServletMappings() { 
     return new String[]{"/"}; 
    } 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class<?>[]{ApplicationConfig.class, WebSecurityConfig.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { WebMvcConfig.class }; 
    } 



    @Override 
    protected Filter[] getServletFilters() { 
     CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); 
     characterEncodingFilter.setEncoding("UTF-8"); 
     characterEncodingFilter.setForceEncoding(true); 

     // you might want to add stuff to enable spring security 


     return new Filter[]{characterEncodingFilter}; 
    } 
} 

@Configuration 
public class ApplicationConfig { 
    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

私は問題が関連していることを疑っていますが、私はここに欠けているものを見ることができません。上記のようにコントローラのパスを変更すると、ログインページにリダイレクトされません。ここで

はの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> 

    <groupId>org.vives</groupId> 
    <artifactId>spring-mvc-quickstart</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>spring-mvc-quickstart</name> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java-version>1.8</java-version> 
     <!-- Override Spring version --> 
     <spring.version>5.0.0.RELEASE</spring.version> 
     <jackson.version>2.9.1</jackson.version> 
     <thymeleaf-extras-java8time-version>3.0.1.RELEASE</thymeleaf-extras-java8time-version> 
     <!-- AssertJ is not a part of Spring IO platform, so the version must be provided explicitly --> 
     <assertj-core-version>3.8.0</assertj-core-version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>7.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>5.0.1.RELEASE</version> 
     </dependency> 
     <!-- Security --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>4.2.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>4.2.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>5.0.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.9.2</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <!-- Avoid issue #72 Could not initialize class org.thymeleaf.templateresolver.ServletContextTemplateResolver due to 'validation is not supported' --> 
      <exclusions> 
       <exclusion> 
        <artifactId>pull-parser</artifactId> 
        <groupId>pull-parser</groupId> 
       </exclusion> 
      </exclusions> 
      <version>5.2.9.Final</version> 
     </dependency> 
    </dependencies> 
    <repositories> 
     <repository> 
      <id>spring-release</id> 
      <name>Spring Release Repository</name> 
      <url>https://repo.spring.io/release</url> 
     </repository> 
    </repositories> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.6</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>7.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

誰かが何が起こっているか、私が間違ってやっているものを私に説明できますか?

答えて

0

私は最終的に私が持っていた問題を解決しました。

セキュリティに関しては、SecurityWebApplicationInitializerを追加するのを忘れてしまい、ナビゲーションとしてリンクが私を助けました。