gpt4 book ai didi

java - 使用 spring-security 登录 Facebook

转载 作者:行者123 更新时间:2023-11-30 07:18:44 25 4
gpt4 key购买 nike

我正在尝试将 facebook 登录添加到我的 Spring 项目中。我能够将用户添加到数据库中,但我仍然需要使用我的 Spring 安全性对他进行身份验证。有谁知道如何做到这一点或者你知道一个好的教程吗?

FBLogin.js

$(document).ready(function () {
function postFunction(type, username, typeUsername, firstname, lastname, id){
$.post(location.protocol + '//' + location.host + '/ProjectTeamF- 1.0/user/addSocial.html', {
type:type,
userName:username,
typeUserName:typeUsername,
firstName:firstname,
lastName:lastname,
id:id
}, function (data) {

window.location = "http://localhost:8080/ProjectTeamF-1.0/" + data;
});
}

FB.Event.subscribe('auth.login', function (response) {
login();
});

FB.Event.subscribe('auth.logout', function (response) {
logout();
});

function login() {
var fbFirstName;
var fbLastName;
var fbId;
var fbUserName;
var fbScreenName;

FB.api('/me', function (response) {
if (response.username == null) {
fbScreenName = response.first_name + response.last_name;
fbUserName = response.first_name + response.last_name;
} else {
fbScreenName = response.username;
fbUserName = response.username;
}
fbFirstName = response.first_name;
fbLastName = response.last_name;
fbId = response.id;
postFunction('Facebook',fbUserName, fbScreenName, fbFirstName, fbLastName, fbId);
});


}

function logout() {

}

})

我的 Controller 中的 AddSocialUser 方法

@RequestMapping(value = "/user/addSocial", method = RequestMethod.POST)
public
@ResponseBody
String addSocialContact(HttpServletRequest request, HttpSession session) {
User user = new User();

if (userService.findUser(request.getParameter("userName")) == null) {
user.setUsername(request.getParameter("userName"));
user.setPassword(request.getParameter("id"));
user.setFirstName((request.getParameter("firstName")));
user.setLastName(request.getParameter("lastName"));

userService.addUser(user);


} else {
user = userService.findUser(request.getParameter("userName"));
}

List<GrantedAuthority> gaList = new ArrayList<GrantedAuthority>();
gaList.add(new GrantedAuthorityImpl("ROLE_USER"));
org.springframework.security.core.userdetails.User usersec = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), true, true, true, true, gaList);
Authentication auth = new UsernamePasswordAuthenticationToken(usersec, user.getPassword(), gaList);
org.springframework.security.core.context.SecurityContext sc = new SecurityContextImpl();
sc.setAuthentication(auth);
org.springframework.security.core.context.SecurityContextHolder.setContext(sc);

return "/ProjectTeamF-1.0/j_spring_security_check";
}

编辑:这是我们的 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.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true">
<intercept-url pattern="/user/admincp-*.html" access="ROLE_USER" />
<intercept-url pattern="/TripParticipants/*.html" access="ROLE_USER" />
<intercept-url pattern="/editUserequipment/*.html" access="ROLE_USER" />
<intercept-url pattern="/TripCategorie/*.html" access="ROLE_USER" />
<intercept-url pattern="/StopPlaats/*.html" access="ROLE_USER" />
<intercept-url pattern="/trip/join/*.html" access="ROLE_USER" />
<intercept-url pattern="/trip/addTrip.html" access="ROLE_USER" />
<form-login login-page="/general/login.html" default-target-url="/general/index.html"
authentication-failure-url="/user/loginfailed.html" />
<logout logout-success-url="/" />
</http>

<authentication-manager >
<authentication-provider >
<password-encoder hash="plaintext"/>
<jdbc-user-service data-source-ref="dataSource"

users-by-username-query="
select username,password ,true
from t_user where username=?"
authorities-by-username-query="
select username, 'ROLE_USER' from t_user where username=? "/>
</authentication-provider>
</authentication-manager>

最佳答案

如果您只想对您的用户进行身份验证,那么就这样做

org.springframework.security.core.userdetails.User user = new User(login, password, true, true, true, true, new ArrayList<GrantedAuthority>());
Authentication auth = new UsernamePasswordAuthenticationToken(user, password, new ArrayList<GrantedAuthority>());
org.springframework.security.core.context.SecurityContext sc = new SecurityContextImpl();
sc.setAuthentication(auth);
org.springframework.security.core.context.SecurityContextHolder.setContext(sc);

并确保当前和所有后续的 http 请求都包含在 spring 安全过滤器中。如果所有用户信息都来自 Facebook,则不需要 DB 用户。

关于java - 使用 spring-security 登录 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15136605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com