简体   繁体   English

Spring Security Servlet登录

[英]Spring Security Servlet Login

I have an existing web application that is covered by spring security. 我有一个Spring安全性涵盖的现有Web应用程序。 I need to add a servlet (or a generic endpoint) that works this way: 我需要添加一个以这种方式工作的servlet(或通用端点):

  • It receives a POST with a json 它收到带有json的POST
  • It processes the data 它处理数据
  • Performs the authentication 执行身份验证
  • Returns a response with other data. 返回带有其他数据的响应。

I've read the documentation about spring security ( http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html ) but I'm a bit confused on how and where implement my code. 我已经阅读了有关Spring安全性的文档( http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html ),但是我对如何以及在何处实现感到困惑我的代码。 I was thinking about using a custom filter in phase 4: UsernamePasswordAuthenticationFilter . 我在考虑在阶段4中使用自定义过滤器: UsernamePasswordAuthenticationFilter However, I'm not really sure because if I understand the framework correctly, the filter is applied to every request, and I need to login only once, and then to be accepted as authenticated. 但是,我不确定,因为如果我正确地理解了框架,则过滤器将应用于每个请求,并且我只需要登录一次,然后就可以被身份验证。

Is the filter the right way or are there better methods? 过滤器是正确的方法还是有更好的方法?

When writing a web service, it is preferable that the client is re-authorized every request. 编写Web服务时,最好对客户端的每个请求都重新授权。 Meaning that the client has to pass their credentials (username/password) every time. 这意味着客户端必须每次都传递其凭据(用户名/密码)。

BTW, if you're writing a web service it might be preferable to to use the BasicAuthenticationEntryPoint over ssl instead of the UsernamePasswordAuthenticaitonFilter. 顺便说一句,如果您正在编写Web服务,则最好在ssl上使用BasicAuthenticationEntryPoint而不是UsernamePasswordAuthenticaitonFilter。

But, to answer your question fully, we used a security/http pattern matcher to do the trick. 但是,为了完全回答您的问题,我们使用了安全性/ http模式匹配器来解决问题。

<security:http pattern="/api/**" create-session="stateless" access-decision-manager-ref="unanimousBased" entry-point-ref="authenticationEntryPoint" >
    <security:intercept-url pattern="/api/**" access="IS_AUTHENTICATED_FULLY,group_User_role_default" />
    <security:custom-filter ref="basicAuthenticationFilter" after="BASIC_AUTH_FILTER" />
</security:http>

<bean id="basicAuthenticationFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
</bean>

<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
    <property name="realmName" value="api" />
</bean>

Finally, it is generally better to authorize first before processing the data. 最后,通常最好在处理数据之前先进行授权。 It seem strange that the data would be processed, then check the authorization to see if the response can be sent, but maybe that is just me. 数据将被处理,然后检查授权以查看是否可以发送响应,这似乎很奇怪,但这也许就是我自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM