0

こんにちは、Spring Security と Spring Oauth は初めてです。Spring Oauth サーバーとリソース サーバーを Java ベースで構成しています。しかし、サーブレットパラメーターを初期化する必要があるなど、xml に基づいて web mvc の一部を構成する必要があります。

以下は私のWeb.xmlです

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app> 

および mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.resource" />

    <mvc:annotation-driven />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/userbase" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

</beans>

エラーログは以下のとおりです。

HTTP Status 500 - No WebApplicationContext found: no ContextLoaderListener registered?

type Exception report

message No WebApplicationContext found: no ContextLoaderListener registered?

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:252)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.68 logs.

Apache Tomcat/7.0.68

SecurityConfiguration.java

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

      @Autowired
      DataSource dataSource;

      private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

      @Autowired
      public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder)
        .usersByUsernameQuery(
         "select username,password, enabled from users where username=?")
        .authoritiesByUsernameQuery(
         "select username, authority from authorities where username=?");
      }


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

    }

****OAuth2ServerConfig.java****

java.security.Principal をインポートします。java.util.UUID をインポートします。

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.web.bind.annotation.RequestMapping;


@Configuration
public class OAuth2ServerConfig {

  @Configuration
  @EnableResourceServer
  protected static class ResourceServer extends ResourceServerConfigurerAdapter {

      @Autowired
      private DataSource dataSource;

      public static final String RESOURCE_ID = "resource_id";

      @Bean
      public JdbcTokenStore tokenStore() {
          return new JdbcTokenStore(dataSource);
      }


      @Bean
      protected AuthorizationCodeServices authorizationCodeServices() {
          return new JdbcAuthorizationCodeServices(dataSource);
      }

      @Override
      public void configure(ResourceServerSecurityConfigurer resources)
              throws Exception {
          resources.tokenStore(tokenStore());
          resources.resourceId(RESOURCE_ID);
      }

      @Override
      public void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().anyRequest().authenticated();
      }


  }

  @RequestMapping("/")
  public String getInfo() {
    return "id: " + UUID.randomUUID() + " content : Spring Oauth2 Welcome first Page";
  }

  @RequestMapping("/user")
  public Principal getUser(Principal principal) {
    return principal;
  }


}

上記で私を助けてください。

4

1 に答える 1

0

springSecurityFilterChain の contextAttribute サフィックスは、ディスパッチャ サーブレット名と一致する必要があるようです。ここであなたの場合、contextAttribute サフィックスはディスパッチャですが、ディスパッチャ サーブレット名は mvc-dispatcher です。以下のように contextAttibute サフィックスを mvc-dispatcher に変更して、問題が解決するかどうかを確認してください。

  <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvc-dispatcher</param-value>
        </init-param>
    </filter>

この情報はhttps://www.txedo.com/blog/spring-security-no-contextloaderlistener-registered/から入手しました

于 2016-05-04T04:18:04.587 に答える