Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Backend
Commits
67e6a1e8
Commit
67e6a1e8
authored
Apr 08, 2014
by
Matija Obreza
Browse files
Attempt to use default URL redirects with Google+ authentication
parent
fb42cb14
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/genesys2/server/servlet/controller/GoogleSocialController.java
View file @
67e6a1e8
...
...
@@ -18,6 +18,7 @@ package org.genesys2.server.servlet.controller;
import
java.io.IOException
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
...
...
@@ -27,6 +28,12 @@ import org.genesys2.server.service.UserService;
import
org.genesys2.server.servlet.util.GoogleOAuthUtil
;
import
org.json.JSONException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.web.authentication.AuthenticationFailureHandler
;
import
org.springframework.security.web.authentication.AuthenticationSuccessHandler
;
import
org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler
;
import
org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler
;
import
org.springframework.social.google.api.Google
;
import
org.springframework.social.google.api.impl.GoogleTemplate
;
import
org.springframework.social.google.api.userinfo.GoogleUserInfo
;
...
...
@@ -40,6 +47,9 @@ public class GoogleSocialController extends BaseController {
@Autowired
private
UserService
userService
;
private
AuthenticationSuccessHandler
authSuccessHandler
=
new
SavedRequestAwareAuthenticationSuccessHandler
();
private
AuthenticationFailureHandler
authFailureHandler
=
new
SimpleUrlAuthenticationFailureHandler
();
@Autowired
private
GoogleOAuthUtil
googleOAuthUtil
;
...
...
@@ -49,7 +59,7 @@ public class GoogleSocialController extends BaseController {
}
@RequestMapping
(
GoogleOAuthUtil
.
LOCAL_GOOGLEAUTH_PATH
)
public
String
googleAuth
(
Model
model
,
HttpServletRequest
request
)
{
public
void
googleAuth
(
Model
model
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
,
ServletException
{
String
accessToken
=
null
;
try
{
...
...
@@ -60,7 +70,8 @@ public class GoogleSocialController extends BaseController {
if
(
accessToken
==
null
)
{
model
.
addAttribute
(
"error"
,
true
);
return
"/login"
;
authFailureHandler
.
onAuthenticationFailure
(
request
,
response
,
new
BadCredentialsException
(
"Could not authenticate you with Google+"
));
return
;
}
Google
google
=
new
GoogleTemplate
(
accessToken
);
...
...
@@ -72,9 +83,10 @@ public class GoogleSocialController extends BaseController {
userService
.
userEmailValidated
(
user
.
getUuid
());
}
googleOAuthUtil
.
googleAuthentication
(
userInfo
);
Authentication
authentication
=
googleOAuthUtil
.
googleAuthentication
(
userInfo
);
return
"redirect:/"
;
// Redirect to URL in session
authSuccessHandler
.
onAuthenticationSuccess
(
request
,
response
,
authentication
);
}
}
src/main/java/org/genesys2/server/servlet/util/GoogleOAuthUtil.java
View file @
67e6a1e8
...
...
@@ -109,19 +109,22 @@ public class GoogleOAuthUtil {
return
"https://accounts.google.com/o/oauth2/auth?"
+
query
;
}
public
void
googleAuthentication
(
GoogleUserInfo
userInfo
)
{
public
Authentication
googleAuthentication
(
GoogleUserInfo
userInfo
)
{
try
{
UserDetails
userDetails
=
userDetailsService
.
loadUserByUsername
(
userInfo
.
getEmail
());
if
(!(
userDetails
.
isEnabled
()
&&
userDetails
.
isAccountNonExpired
()
&&
userDetails
.
isAccountNonLocked
()
&&
userDetails
.
isCredentialsNonExpired
()))
{
LOG
.
warn
(
"Google login canceled: Account currently not available: "
+
userInfo
.
getEmail
());
return
;
return
null
;
}
Authentication
authentication
=
new
UsernamePasswordAuthenticationToken
(
userDetails
,
null
,
userDetails
.
getAuthorities
());
SecurityContextHolder
.
getContext
().
setAuthentication
(
authentication
);
return
authentication
;
}
catch
(
UsernameNotFoundException
e
)
{
LOG
.
warn
(
"Authentication with Google+ failed: No such user "
+
userInfo
.
getEmail
());
return
null
;
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment