2014-02-17 98 views
2

我是Spring Security的新手,我试图设置对PostgreSQL数据库的认证/授权。我遵循前三章here并获得了内存中用户名/密码的正常工作。创建模式(here)所需的表,然后用所需的春天所有的豆子一起设立在Tomcat中(here)JNDI数据源后,登录正与此消息失败:Spring Security with JDBC Authentication - No AuthenticationProvider found

你的登录尝试不成功,再试一次。

原因:没有的AuthenticationProvider发现 org.springframework.security.authentication.UsernamePasswordAuthenticationToken

这里的豆在我的servlet-context.xml中定义:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/TyedArtDB"/> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

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

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <beans:bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> 
     <beans:property name="dataSource" ref="dataSource"/> 
    </beans:bean> 

    <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> 

    <beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
     <beans:property name="userDetailsService" ref="userDetailsService"/> 
     <beans:property name="passwordEncoder" ref="passwordEncoder"/> 
    </beans:bean> 

    <beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"> 
     <beans:property name="providers"> 
      <beans:list> 
       <beans:ref bean="daoAuthenticationProvider"/> 
      </beans:list> 
     </beans:property> 
    </beans:bean> 

</beans:beans> 

这是我的SecurityConfig等级:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
//  auth 
//   .inMemoryAuthentication() 
//    .withUser("rob").password("22").roles("ADMIN"); 

     InitialContext ctx = new InitialContext(); 
     DataSource dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/TyedArtDB"); 

     auth 
      .jdbcAuthentication() 
       .dataSource(dataSource) 
       .withDefaultSchema() 
       .passwordEncoder(new BCryptPasswordEncoder()); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/manage/**").hasRole("ADMIN") 
       .and() 
      .formLogin(); 
    } 
} 

任何想法我在做什么克错了吗?

回答

3

我刚刚用JDBC配置了Security。我使用java注释来配置我的应用程序。我注意到了几件事情:

.withDefaultSchema() 

对我来说,意外的是。我配置了一个数据源,但是我没有创建默认表。 Spring连接到我的数据源并自动创建模式。我注意到你手动创建了模式。这也是我的期望。该文档似乎是不明确的,所以我只是运行我的应用程序而不创建表格。 Voila Spring为我创建了数据库表。也许模式不匹配(参见我的下一部分,我发现文档稍微过时)。

接下来,我将Spring Security添加到小型Spring MVC应用程序中。春季博客包含正确的配置注释:

@EnableWebMvcSecurity 

让我知道你是否要我发布我的安全配置。它不是java的xml。因此,我不知道这是否会有所帮助。

+0

谢谢,这两个项目正是我所需要的!注入数据源时是否遇到问题?我有另一个帖子在这里:http://stackoverflow.com/questions/21826538/spring-jndi-datasource-no-qualifying-bean-of-type-javax-sql-datasource-found –

+0

很高兴有帮助。我将数据源配置为Hibernate上的JPA的一部分。我使用Spring而不是JNDI来访问数据源。 I.E.数据源用@Bean注释。我有相同的设置git hub项目(减去弹簧安全性。你想要的网址? – lorinpa

相关问题