session: " + session.getId() + "
Logout";
35 | }
36 |
37 | @GetMapping("/logout")
38 | public String logout() {
39 | Subject currentUser = SecurityUtils.getSubject();
40 | currentUser.logout();
41 | return "You've logout";
42 | }
43 |
44 | private void checkSession(Session session) {
45 | // Try to set value to redis-based session
46 | session.setAttribute("someKey", "aValue");
47 | String value = (String) session.getAttribute("someKey");
48 | if (!value.equals("aValue")) {
49 | log.info("Cannot retrieved the correct value! [" + value + "]");
50 | }
51 | }
52 |
53 | private void checkAuthorization(Subject currentUser) {
54 | // say who they are:
55 | // print their identifying principal (in this case, a username):
56 | log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
57 |
58 | //test a role:
59 | if ( currentUser.hasRole( "schwartz" ) ) {
60 | log.info("May the Schwartz be with you!" );
61 | } else {
62 | log.info( "Hello, mere mortal." );
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/org/crazycake/shiroredisspringboottutorial/model/LoginForm.java:
--------------------------------------------------------------------------------
1 | package org.crazycake.shiroredisspringboottutorial.model;
2 |
3 | public class LoginForm {
4 | private String username;
5 | private String password;
6 |
7 | public String getUsername() {
8 | return username;
9 | }
10 |
11 | public void setUsername(String username) {
12 | this.username = username;
13 | }
14 |
15 | public String getPassword() {
16 | return password;
17 | }
18 |
19 | public void setPassword(String password) {
20 | this.password = password;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/org/crazycake/shiroredisspringboottutorial/model/UserInfo.java:
--------------------------------------------------------------------------------
1 | package org.crazycake.shiroredisspringboottutorial.model;
2 |
3 | import java.io.Serializable;
4 |
5 | public class UserInfo implements Serializable {
6 |
7 | private Integer id;
8 |
9 | private String username;
10 |
11 | public Integer getId() {
12 | return id;
13 | }
14 |
15 | public void setId(Integer id) {
16 | this.id = id;
17 | }
18 |
19 | public String getUsername() {
20 | return username;
21 | }
22 |
23 | public void setUsername(String username) {
24 | this.username = username;
25 | }
26 | }
--------------------------------------------------------------------------------
/src/main/java/org/crazycake/shiroredisspringboottutorial/realm/ExampleRealm.java:
--------------------------------------------------------------------------------
1 | package org.crazycake.shiroredisspringboottutorial.realm;
2 |
3 | import org.apache.shiro.authc.*;
4 | import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
5 | import org.apache.shiro.authz.AuthorizationInfo;
6 | import org.apache.shiro.authz.SimpleAuthorizationInfo;
7 | import org.apache.shiro.realm.AuthorizingRealm;
8 | import org.apache.shiro.subject.PrincipalCollection;
9 | import org.crazycake.shiroredisspringboottutorial.model.UserInfo;
10 | import org.springframework.stereotype.Repository;
11 |
12 | import javax.annotation.PostConstruct;
13 | import java.util.ArrayList;
14 | import java.util.Arrays;
15 | import java.util.List;
16 | import java.util.concurrent.ThreadLocalRandom;
17 |
18 | /**
19 | * This realm is only for tutorial
20 | * @author Alex Yang
21 | *
22 | */
23 | @Repository
24 | public class ExampleRealm extends AuthorizingRealm {
25 |
26 | @Override
27 | protected AuthorizationInfo doGetAuthorizationInfo(
28 | PrincipalCollection principals) {
29 | SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();
30 | // only for tutorial
31 | authInfo.addRoles(new ArrayListshiro-redis tutorial
3 |
19 |
--------------------------------------------------------------------------------
/src/test/java/org/crazycake/shiroredisspringboottutorial/ShiroRedisSpringBootTutorialApplicationTests.java:
--------------------------------------------------------------------------------
1 | package org.crazycake.shiroredisspringboottutorial;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ShiroRedisSpringBootTutorialApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------