【问题标题】:Spring Security to user JPA connectionSpring Security 到用户 JPA 连接
【发布时间】:2013-02-13 13:24:23
【问题描述】:

我尝试从 StackOverflow (Use Spring Security with JPA) 关注这篇帖子,但没有成功。

我实现了一个 UserDetailsS​​ervice:

import javax.inject.Inject;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.boss.mrfoods.dao.UserDao;
import com.boss.mrfoods.entity.User;

@Service
public class LoginController implements UserDetailsService {

    @Inject
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        User user = userDao.getForUsername(username);

        System.out.println("USERNAME: " + username);
        System.out.println("USER: " + user);
        System.out.println("ROLES:" + user.getRoles());

        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.getRoles());
    }

}

并在其中一个 Spring XML 配置文件中引用它,如下所示:

<debug />

<global-method-security pre-post-annotations="enabled" />

<http pattern="/resources/**" security="none" />
<http pattern="/pages/loggedout.xhtml" security="none" />
<http pattern="/pages/timeout.xhtml" security="none" />

<http use-expressions="true">
    <intercept-url pattern="/pages/admin/**" access="hasRole('supervisor')" />
    <intercept-url pattern="/pages/user/**" access="isAuthenticated()" />
    <intercept-url pattern="/**" access="permitAll" />
    <form-login />
    <logout logout-success-url="/pages/loggedout.xhtml" delete-cookies="JSESSIONID" />
    <remember-me />
</http>

<beans:bean id="customUserDetailsService" class="com.boss.mrfoods.controller.LoginController" />

<authentication-manager>
    <authentication-provider user-service-ref="customUserDetailsService">
        <password-encoder hash="plaintext" />
    </authentication-provider>
</authentication-manager>

什么都没有发生。没有例外,我的 UserDetailsS​​ervice 实现永远不会被调用。

我想要归档的是 Spring Security 使用我的 JPA 连接/事务来查找用户/角色。我缺少配置吗?如果我什至没有得到异常,我从哪里开始寻找问题。

到目前为止我发现的是:我的 userDao 为空。对象注入不起作用。 Inject 无法构建对象。为什么?

感谢您阅读本文。

【问题讨论】:

  • 您是如何尝试进行身份验证的?您的安全配置中有http 元素吗?
  • 编辑了问题以显示更多 XML 配置文件。

标签: jsf jpa spring-security


【解决方案1】:

您能否详细说明您是如何登录到该应用程序的。您是否直接访问用户页面并期待登录页面?

您是否尝试过这样设置登录页面,

 <form-login
        login-page="/login.html"
        login-processing-url="/j_spring_security_check.action"
        authentication-failure-url="/login_error.html"
        default-target-url="/home.html"
        always-use-default-target="true"/>

【讨论】:

  • 我使用的是默认登录表单(您可以通过我的 XML 中的“”行来实现这一点。)。尝试访问其中一个受保护的页面时,该表单正确显示,但是当我尝试登录时,我没有通过身份验证,并且我的断点和系统输出永远不会被调用。
  • 您能否提供更多有关您登录时发生的情况的详细信息。您是否看到错误页面。如果页面保持在同一屏幕上,您是否在服务器上看到任何异常。如果上述情况均未发生,请尝试在诸如 fire bug 等工具中监控请求,以监控提交登录表单时发生的情况。
  • 默认登录页面通知 2 件事:“您的登录尝试不成功,请重试。” “原因:空”。 Spring-Security 调试已激活,但未显示任何错误。系统总是为登录尝试返回一个空用户。 (如果重要的话,当使用 XML 中定义的静态用户时,一切正常)。
  • 这就是我将如何调试它,监视登录请求上的请求和响应标头,并查看它与您第二次访问时实际工作时的不同之处。您可以在 forefox 或 chrome 中使用 firebug 来查看请求和响应标头。第一次和第二次调用它的方式肯定有些不同。
  • 我发现问题出在哪里:@Inject。每当我尝试在我的自定义 UserDetailService 中注入任何 bean 时,Spring 不再调用它。现在我不知道如何解决这个问题。
猜你喜欢
  • 2013-05-01
  • 2019-04-06
  • 2012-04-01
  • 2018-12-31
  • 2017-02-23
  • 2015-01-18
  • 1970-01-01
  • 2022-09-28
  • 2015-06-08
相关资源
最近更新 更多