|
2 | 2 |
|
3 | 3 | import java.text.ParseException; |
4 | 4 | import java.text.SimpleDateFormat; |
| 5 | +import java.util.Arrays; |
5 | 6 | import java.util.Date; |
6 | 7 | import java.util.HashMap; |
7 | 8 | import java.util.List; |
8 | 9 | import java.util.Map; |
9 | 10 |
|
10 | | -import javax.servlet.http.HttpSession; |
11 | | - |
12 | 11 | import org.baeldung.persistence.dao.PostRepository; |
13 | 12 | import org.baeldung.persistence.dao.UserRepository; |
14 | 13 | import org.baeldung.persistence.model.Post; |
|
18 | 17 | import org.slf4j.LoggerFactory; |
19 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
20 | 19 | import org.springframework.http.HttpStatus; |
| 20 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 21 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 22 | +import org.springframework.security.core.context.SecurityContextHolder; |
21 | 23 | import org.springframework.security.oauth2.client.OAuth2RestTemplate; |
22 | 24 | import org.springframework.security.oauth2.common.OAuth2AccessToken; |
23 | 25 | import org.springframework.stereotype.Controller; |
@@ -48,13 +50,11 @@ public class RedditController { |
48 | 50 | @Autowired |
49 | 51 | private PostRepository postReopsitory; |
50 | 52 |
|
51 | | - @RequestMapping("/info") |
52 | | - public final String getInfo(HttpSession session) { |
| 53 | + @RequestMapping("/login") |
| 54 | + public final String redditLogin() { |
53 | 55 | final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class); |
54 | | - final String name = node.get("name").asText(); |
55 | | - addUser(name, redditRestTemplate.getAccessToken()); |
56 | | - session.setAttribute("username", name); |
57 | | - return "reddit"; |
| 56 | + loadAuthentication(node.get("name").asText(), redditRestTemplate.getAccessToken()); |
| 57 | + return "redirect:home.html"; |
58 | 58 | } |
59 | 59 |
|
60 | 60 | @RequestMapping(value = "/submit", method = RequestMethod.POST) |
@@ -207,25 +207,26 @@ private final String parseResponse(final JsonNode node) { |
207 | 207 | } |
208 | 208 | } |
209 | 209 |
|
210 | | - private final void addUser(final String name, final OAuth2AccessToken token) { |
| 210 | + private final void loadAuthentication(final String name, final OAuth2AccessToken token) { |
211 | 211 | User user = userReopsitory.findByUsername(name); |
212 | 212 | if (user == null) { |
213 | 213 | user = new User(); |
214 | 214 | user.setUsername(name); |
215 | | - user.setAccessToken(token.getValue()); |
216 | | - user.setRefreshToken(token.getRefreshToken().getValue()); |
217 | | - user.setTokenExpiration(token.getExpiration()); |
218 | 215 | } |
219 | 216 |
|
220 | | - final String needsCaptchaResult = needsCaptcha(); |
221 | | - if (needsCaptchaResult.equalsIgnoreCase("true")) { |
| 217 | + if (needsCaptcha().equalsIgnoreCase("true")) { |
222 | 218 | user.setNeedCaptcha(true); |
223 | 219 | } else { |
224 | 220 | user.setNeedCaptcha(false); |
225 | 221 | } |
| 222 | + |
226 | 223 | user.setAccessToken(token.getValue()); |
227 | 224 | user.setRefreshToken(token.getRefreshToken().getValue()); |
| 225 | + user.setTokenExpiration(token.getExpiration()); |
228 | 226 | userReopsitory.save(user); |
| 227 | + |
| 228 | + final UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, token.getValue(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"))); |
| 229 | + SecurityContextHolder.getContext().setAuthentication(auth); |
229 | 230 | } |
230 | 231 |
|
231 | 232 | } |
0 commit comments