【问题标题】:Spring MVC + Spring Security - Controller Authentication using JSONSpring MVC + Spring Security - 使用 JSON 的控制器身份验证
【发布时间】:2014-02-04 07:37:00
【问题描述】:

我想使用简单的 Spring 控制器来使用 Spring Security 对用户进行身份验证。

我的控制器

@Controller
@Scope("request")
public class Authenticator {

private String username;
private String password;

@Autowired
private AuthenticationManager authenticationManager;

@RequestMapping(value = "/login", method = {RequestMethod.POST })
public @ResponseBody String authentication(@RequestParam("login") String userName,
        @RequestParam("password") String password, HttpServletRequest request) {

    this.username = userName;
    this.password = password;

    Authentication authenticationToken = new UsernamePasswordAuthenticationToken(
            userName, password);
    try {

        Authentication authentication = authenticationManager
                .authenticate(authenticationToken);


        SecurityContext securityContext = SecurityContextHolder
                .getContext();

        securityContext.setAuthentication(authentication);

        HttpSession session = request.getSession(true);
        session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);

        return "sucess";
    } catch (AuthenticationException ex) {
        return "fail " + ex.getMessage();
    }

}

我的 spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 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/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">


<http pattern="/resources/**" security="none" />

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login" access="permitAll" />

    <intercept-url pattern="/logout" access="permitAll" />

    <intercept-url pattern="/accessdenied" access="permitAll" />

    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

    <form-login />

    <logout logout-success-url="/logout" />


    <!-- <session-management invalid-session-url="/loginlimmit">
        <concurrency-control error-if-maximum-exceeded="true"
            max-sessions="1" />
    </session-management> -->

</http>

<authentication-manager>
    <authentication-provider>

        <user-service>
            <user name="a" password="a" authorities="ROLE_USER" />
        </user-service>

    </authentication-provider>
</authentication-manager>

这很好用

1 - 如果我尝试访问 http: //localhost/app 将重定向到 http: //localhost/app/spring_security_login 以按预期登录

2 - 如果我将 POST 方法发送到 http: //localhost/app/login 有效,我会收到消息成功或失败,使用凭据 username=a 和 password=a 在 Spring-security.xml 中定义提供者,因此它确实使用 spring .security 进行身份验证。

问题

发送 POST 方法并获得登录成功后,如果我尝试访问 http://localhost/app 将重定向到 http://localhost/app/spring_security_login ,所以我无法理解,因为身份验证工作正常!

如何在其他控制器中获得用户身份验证?

我的目标是使用 Spring MVC 开发一个应用程序,但我不会将其用作标准 Web 应用程序,它将像后端应用程序一样工作,而前端将是其他应用程序,例如桌面、移动、vaadin 框架,这些应用程序将使用JSON,Spring MVC 可以正常工作,但我需要实现身份验证,在这种情况下,使用 Spring Security。

有什么问题吗?

【问题讨论】:

  • 为什么? Spring Security 已经完成了所有这些(以及更多)工作,您为什么要针对/围绕而不是使用框架?

标签: java spring spring-mvc spring-security


【解决方案1】:

您不需要设置控制器,Spring Security 有一个过滤器链用于验证,您只需将您的用户名/密码发布到该链。

<form-login 
    password-parameter="password" --> password field
    username-parameter="username" --> username field
    login-processing-url="/security/j_spring_security_check"  --> set your form's action attribute to this URL, no need to implement anything at that URL
    login-page="/login" --> login page 
/>

【讨论】:

  • 桌面或移动应用程序中没有表单属性
  • @KJEjava48 你用username 和password 参数执行POST 到/security/j_spring_security_check。
  • restTemplate 允许我仅使用一个参数使用 POST。所以我需要使用 Mulivaluemap 或 json 对象传递此用户名和密码。然后 spring security 将如何从地图中拆分此用户名和密码还是json???
  • @KJEjava48,你的问题是如何使用rest模板,我建议你google一下。
  • 不。我在问你这个spring security将如何从post请求中识别json对象的用户名和密码
【解决方案2】:
1 - if I try to access http: //localhost/app is redirect to http: //localhost/app/spring_security_login to login as expected

如果正常的登录方式有效并且在您请求http: //localhost/app 时它没有再次重定向到登录页面,那么您的配置是好的。

After send POST method and get login sucess, if I try to acess http: //localhost/app is redirect to http: //localhost/app/spring_security_login , so I cant undestand beacause the authentication worked fine!

对于每个请求,应用程序都将其视为新会话。如果您已经在 web.xml 中配置了 Spring Security 过滤器(这使得第 1 点可以正常工作)应该为您创建 Session,所以您可以在控制器中调用 getSession() 而不是 getSession(true)。

只需记录请求之间的会话 id 并查看会话是否不同。这可能是由于您从客户端调用的方式。

【讨论】:

  • 您能提供任何示例如何避免从控制器创建新会话吗?我已经更改了控制器的代码,但它没有工作。
  • 好的,对于每个请求,您将获得不同的会话?
  • 没错,我怎样才能避免这种情况?
  • 你必须在你的客户端处理这个。检查这个stackoverflow.com/questions/6159848/…
猜你喜欢
  • 2013-10-30
  • 2016-04-30
  • 1970-01-01
  • 2013-01-15
  • 2013-01-02
  • 2011-11-14
  • 2011-06-04
  • 2016-09-09
相关资源
最近更新 更多