【发布时间】:2017-11-11 14:12:51
【问题描述】:
我们有一个应用程序使用 spring 基本身份验证来确保安全。该应用程序不是基于 Spring MVC 构建的。我们将聚合物用作前端,并将服务公开为基于休息的服务。
要求是实现一个登录表单,点击提交按钮需要调用一个应该发布用户名/密码的javascript。
有没有办法从 javascript 将凭据传递给 spring 基本身份验证 servlet,以便它验证请求。我们已经实现了 AuthenticationProvider 身份验证来执行验证。
认证java代码
import org.springframework.security.authentication.*;
import org.springframework.security.core.*;
import java.util.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import com.lifesciencemeta.ls.User;
import com.lifesciencemeta.ls.LifeScienceServiceMaster;
import com.lifesciencemeta.ls.LifeScienceConstants;
public class SpringBasicAuthentication implements AuthenticationProvider {
public LifeScienceServiceMaster lifeScienceService;
@Context
UriInfo lsUri;
public LifeScienceServiceMaster getLifeScienceService() {
return lifeScienceService;
}
public void setLifeScienceService(LifeScienceServiceMaster lifeScienceService) {
this.lifeScienceService = lifeScienceService;
}
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
String principal = (String) auth.getPrincipal();
String credential = (String) auth.getCredentials();
User u = lifeScienceService.authenticateUser(principal, credential);
if (u == null)
throw new BadCredentialsException(LifeScienceConstants.getMsg(“Auth Failed"));
else {
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
String role = u.getRole().getName();
if(role == null) {
throw new BadCredentialsException(LifeScienceConstants.getMsg(“Auth Failed"));
}
grantedAuths.add(new SimpleGrantedAuthority(role));
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
principal, credential, grantedAuths);
result.setDetails(u);
return result;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
Web.xml
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" 版本="2.5"> org.springframework.web.context.ContextLoaderListener 上下文配置位置 /WEB-INF/context.xml springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain / 泽西 REST 服务 com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages com.lifesciencemeta.ls.restService.Invoke com.sun.jersey.api.json.POJOMappingFeature 真的 1 泽西 REST 服务 /休息/
安全性.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled" pre-post-annotations="enabled" />
<security:http>
<security:intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN"/>
<security:http-basic />
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="SpringBasicAuthentication" />
</security:authentication-manager>
<bean id="SpringBasicAuthentication"
class="com.lifesciencemeta.ls.SpringBasicAuthentication" >
<property name="lifeScienceService" ref="lsLifeScienceServiceImpl"/>
</bean>
</beans>
【问题讨论】:
-
所以您使用的是 Spring Security,对吗?此类任务的端点是 /login。
-
感谢您的建议!
标签: javascript spring authentication