- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Facebook token
通过 Spring Security
验证我的 REST 后端。您能否详细说明如何将此安全性集成到我的 Spring 应用程序中。
我想使用与 Spring Social Security 相同的用户管理。 UserConnection
表和本地用户表。
最佳答案
您可以从以下网址下载代码示例:
https://github.com/ozgengunay/FBSpringSocialRESTAuth
我们一直在寻找一种“Spring”解决方案,它使用 REST 客户端已经拥有的 Facebook OAuth token 来保护我们的 REST 后端。例如:您有一个移动应用程序,在应用程序本身中实现了 Facebook Connect SDK,另一方面,您有一个提供 REST API 的后端。您想要使用 Facebook OAuth token 对 REST API 调用进行身份验证。该解决方案实现了这种场景。
不幸的是,Spring Social Security Framework 仅保护您的有状态 HTTP 请求,而不是您的无状态 REST 后端。
这是 spring 社会保障框架的扩展,由一个组件组成:FacebookTokenAuthenticationFilter。此过滤器拦截所有 REST 调用。客户端应在每个请求中将 Facebook OAuth token 作为“input_token”参数发送到 url 中,因为 REST API 本质上是无限制的。过滤器查找此 token 并通过“debug_token”Graph Api 调用对其进行验证。如果 token 通过验证,过滤器会尝试将用户与本地用户管理系统相匹配。如果还没有这样的用户注册,过滤器会将该用户注册为新用户。
如果您还拥有 REST API 以外的服务(例如 Web 后端),则可以将此过滤器与 Spring Social Security 的标准 SocialAuthenticationFilter 一起使用。因此,您可以使用相同的用户管理系统。
1) 在 MYSQL 中创建用户表如下:
CREATE TABLE IF NOT EXISTS `user` (
`id` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL COMMENT 'unique',
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`role` varchar(255) NOT NULL,
`sign_in_provider` varchar(20) DEFAULT NULL,
`creation_time` datetime NOT NULL,
`modification_time` datetime NOT NULL,
`status` varchar(20) NOT NULL COMMENT 'not used',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
);
2) 在 context.xml 中配置您的数据源:
tomcat 中的 context.xml :
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000"
name="jdbc/thingabled" password="..." type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/..." username="..."/>
3) Spring 配置:我们配置 spring security 来拦截以“protected”开头的 URL,由 FacebookTokenAuthenticationFilter 进行身份验证。授权将由“ROLE_USER_REST_MOBILE”角色完成。
<security:http use-expressions="true" pattern="/protected/**"
create-session="never" entry-point-ref="forbiddenEntryPoint">
<security:intercept-url pattern="/**"
access="hasRole('ROLE_USER_REST_MOBILE')" />
<!-- Adds social authentication filter to the Spring Security filter chain. -->
<security:custom-filter ref="facebookTokenAuthenticationFilter"
before="FORM_LOGIN_FILTER" />
</security:http>
<bean id="facebookTokenAuthenticationFilter"
class="com.ozgen.server.security.oauth.FacebookTokenAuthenticationFilter">
<constructor-arg index="0" ref="authenticationManager" />
<constructor-arg index="1" ref="userIdSource" />
<constructor-arg index="2" ref="usersConnectionRepository" />
<constructor-arg index="3" ref="connectionFactoryLocator" />
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider
ref="socialAuthenticationProvider" />
</security:authentication-manager>
<!-- Configures the social authentication provider which processes authentication
requests made by using social authentication service (FB). -->
<bean id="socialAuthenticationProvider"
class="org.springframework.social.security.SocialAuthenticationProvider">
<constructor-arg index="0" ref="usersConnectionRepository" />
<constructor-arg index="1" ref="simpleSocialUserDetailsService" />
</bean>
<bean id="forbiddenEntryPoint"
class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" />
<!-- This bean determines the account ID of the user.-->
<bean id="userIdSource"
class="org.springframework.social.security.AuthenticationNameUserIdSource" />
<!-- This is used to hash the password of the user. -->
<bean id="passwordEncoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<constructor-arg index="0" value="10" />
</bean>
<!-- This bean encrypts the authorization details of the connection. In
our example, the authorization details are stored as plain text. DO NOT USE
THIS IN PRODUCTION. -->
<bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors"
factory-method="noOpText" />
4) FacebookTokenAuthenticationFilter 将拦截所有无状态 REST 请求,以使用有效的 Facebook token 对请求进行身份验证。检查 Facebook token 是否有效。如果 Facebook token 无效,则请求将被拒绝。如果 Facebook token 有效,则过滤器将尝试通过 SimpleSocialUserDetailsService 对请求进行身份验证。如果用户和用户连接数据不可用,则会创建一个新用户(通过 UserService)和 UserConnection。
private Authentication attemptAuthService(...) {
if (request.getParameter("input_token") == null) {
throw new SocialAuthenticationException("No token in the request");
}
URIBuilder builder = URIBuilder.fromUri(String.format("%s/debug_token", "https://graph.facebook.com"));
builder.queryParam("access_token", access_token);
builder.queryParam("input_token", request.getParameter("input_token"));
URI uri = builder.build();
RestTemplate restTemplate = new RestTemplate();
JsonNode resp = null;
try {
resp = restTemplate.getForObject(uri, JsonNode.class);
} catch (HttpClientErrorException e) {
throw new SocialAuthenticationException("Error validating token");
}
Boolean isValid = resp.path("data").findValue("is_valid").asBoolean();
if (!isValid)
throw new SocialAuthenticationException("Token is not valid");
AccessGrant accessGrant = new AccessGrant(request.getParameter("input_token"), null, null,
resp.path("data").findValue("expires_at").longValue());
Connection<?> connection = ((OAuth2ConnectionFactory<?>) authService.getConnectionFactory())
.createConnection(accessGrant);
SocialAuthenticationToken token = new SocialAuthenticationToken(connection, null);
Assert.notNull(token.getConnection());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null || !auth.isAuthenticated()) {
return doAuthentication(authService, request, token);
} else {
addConnection(authService, request, token);
return null;
}
}
5) 项目中的其他重要部分:
用户:映射“用户”表的实体。
@Entity
@Table(name = "user")
public class User extends BaseEntity {
@Column(name = "email", length = 255, nullable = false, unique = true)
private String email;
@Column(name = "first_name", length = 255, nullable = false)
private String firstName;
@Column(name = "last_name", length = 255, nullable = false)
private String lastName;
@Column(name = "password", length = 255)
private String password;
@Column(name = "role", length = 255, nullable = false)
private String rolesString;
@Enumerated(EnumType.STRING)
@Column(name = "sign_in_provider", length = 20)
private SocialMediaService signInProvider;
...
}
UserRepository:Spring Data JPA 存储库,它将使我们能够在“用户”实体上运行 CRUD 操作。
public interface UserRepository extends JpaRepository<User, String> {
public User findByEmailAndStatus(String email,Status status);
public User findByIdAndStatus(String id,Status status);
}
UserService:这个 spring 服务将用于创建一个新的用户帐户,将数据插入“用户”表。
@Service
public class UserService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserRepository repository;
@Transactional
public User registerNewUserAccount(RegistrationForm userAccountData) throws DuplicateEmailException {
LOGGER.debug("Registering new user account with information: {}", userAccountData);
if (emailExist(userAccountData.getEmail())) {
LOGGER.debug("Email: {} exists. Throwing exception.", userAccountData.getEmail());
throw new DuplicateEmailException("The email address: " + userAccountData.getEmail() + " is already in use.");
}
LOGGER.debug("Email: {} does not exist. Continuing registration.", userAccountData.getEmail());
User registered =User.newEntity();
registered.setEmail(userAccountData.getEmail());
registered.setFirstName(userAccountData.getFirstName());
registered.setLastName(userAccountData.getLastName());
registered.setPassword(null);
registered.addRole(User.Role.ROLE_USER_WEB);
registered.addRole(User.Role.ROLE_USER_REST);
registered.addRole(User.Role.ROLE_USER_REST_MOBILE);
if (userAccountData.isSocialSignIn()) {
registered.setSignInProvider(userAccountData.getSignInProvider());
}
LOGGER.debug("Persisting new user with information: {}", registered);
return repository.save(registered);
}
....
}
SimpleSocialUserDetailsService :SocialAuthenticationProvider 将使用此 Spring 服务来验证用户的 userId。
@Service
public class SimpleSocialUserDetailsService implements SocialUserDetailsService {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSocialUserDetailsService.class);
@Autowired
private UserRepository repository;
@Override
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException, DataAccessException {
LOGGER.debug("Loading user by user id: {}", userId);
User user = repository.findByEmailAndStatus(userId, Status.ENABLED);
LOGGER.debug("Found user: {}", user);
if (user == null) {
throw new UsernameNotFoundException("No user found with username: " + userId);
}
ThingabledUserDetails principal = new ThingabledUserDetails(user.getEmail(),user.getPassword(),user.getAuthorities());
principal.setFirstName(user.getFirstName());
principal.setId(user.getId());
principal.setLastName(user.getLastName());
principal.setSocialSignInProvider(user.getSignInProvider());
LOGGER.debug("Found user details: {}", principal);
return principal;
}
}
您可以从以下网址下载代码示例:
关于用于使用 Facebook token 进行身份验证的无状态 REST 端点的 Spring 社交身份验证过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35911723/
在 spring boot 2 应用程序中,我正在尝试访问执行器端点/beans,就像我之前在 Spring boot 1.5.* 应用程序中所做的那样。但我做不到。此外,我没有在 log.INFO
我正在为资源 items 编写端点,它是 applications 的子资源,如下所示:applications/{:id}/items。项目和应用程序都具有除名称之外的其他属性。 我创造了 GET
我正在创建一个 API,其中基于经过身份验证的用户可以更改对象的不同属性的权限。 解决这个问题的常用方法是什么? 我应该有这样的端点吗 /admin/users 和 /users 具有不同的 API
也许(希望如此)我错过了一些非常简单的东西,但我似乎无法弄清楚。 我有一组我想放在 nghttpx 代理后面的 gRPC 服务。为此,我需要能够使用非根 url 上的 channel 配置我的客户端。
我没有找到法定存款的历史记录(来自银行卡), 这里只有加密存款:https://prnt.sc/ttdwc2= ) 例如,在我的银行帐户界面中,我在 5 月 12 日找到了存款,但在这里找不到...
我很好奇普罗米修斯的工作原理。使用 Prometheus 界面,我可以看到一个下拉列表,我认为其中包含所有可用的指标。但是,我无法访问列出所有抓取的指标的指标端点。 http://targethost
是否可以从 apollo-server-express 上的 GraphQL 端点触发浏览器中的文件下载?应用? 我有一个用标准 express 写的端点 app.get函数(见下文),但我想利用 G
有谁知道在一个请求中获取您上传到媒体库的所有图像的端点吗?我将 next js 与 Strapi 一起使用,需要一种方法来从媒体库中获取所有图像,但似乎没有任何相关文档 最佳答案 /api/上传 GE
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我编写了一个简单的 HTTP 监听器并将其部署在 Heroku 应用程序中。我确保在 $PORT 中绑定(bind)端口。 现在,我尝试使用 url name-of-my-app.herokuapp.
我正在尝试构建一个安全系统,用于将数据从客户端 Android 应用程序传输到运行 PHP 的网络服务器。 我想做的是确保系统是加密安全的,这样来自应用程序的消息可以被验证为实际上来自应用程序本身,而
我是 Go 编程语言的新手。尝试使用 gousb访问爱普生收据打印机。 因此,我从存储库中获取了一些示例代码,并对其进行了一些调整,以验证我是否可以访问打印机。 我可以看到打印机并枚举端点。我收到此输
我正在使用 azure API 端点。 ....azure-api.net/...。当我尝试使用 Charles 代理查看 HTTP 请求/响应时,HTTP 响应为空。当我关闭代理时,该请求有效。 我
我正在关注这个tutorial了解用户成功登录后如何获取 token 。 到目前为止我已完成的步骤: 我已使用此 URL 注册了自己(用户名和密码):https://MyCompany.b2clogi
给定一个以 .svc 结尾且应该运行 SOAP 网络服务的 URL,我如何从中获取一些数据? 我试过: 通过网络浏览器访问它 通过 Python 的库 Zeep 访问它 通过 Microsoft 实用
我认为公共(public) REST API(例如注册端点)无法验证用户身份是否正确?例如,我们的端点应该只接受来 self 们的移动应用程序和 future 网络应用程序的请求。 我很确定这在逻辑上
在自托管服务中,我想使用 App.config 中指定的端点(如果存在),或者如果 App.config 为空则使用代码中指定的默认端点。我该怎么做? 编辑:澄清一下,这是在服务器(服务)端使用 Se
我需要在我的后端服务器中实现实时开发者通知,以了解我的用户所做的任何购买修改(暂停帐户、续订订阅等)。我的后端服务器是用 Delphi 制作的,没有现成的 Delphi 库,但是,我可以制作一个 HT
我创建了一个 Kubernetes 服务: [root@Infra-1 kubernetes]# kubectl describe service gitlab Name: git
我正在开发一个应用程序,我需要将对象列表传递给 REST 端点,该端点将进行一些计算并将结果返回给调用者。 问题更多是关于如何处理这种情况的哲学问题? 在 GET 请求中传递大量有效负载是一个坏主意。
我是一名优秀的程序员,十分优秀!