Skip to content

Commit cec17bd

Browse files
SeunMattzhendrikse
authored andcommitted
Updated Example Code for Apache Shiro (eugenp#2501)
* added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code * added example code for bytebuddy * added example code for PCollections * update pom * refactored tests for PCollections * spring security xml config * spring security xml config * remove redundant comment * example code for apache-shiro * updated example code for Vavr Collections * updated Vavr's Collection example * updated Vavr Collection file * updated example code for Apache Shiro
1 parent 8180274 commit cec17bd

File tree

8 files changed

+269
-3
lines changed

8 files changed

+269
-3
lines changed

apache-shiro/pom.xml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<version>1.0-SNAPSHOT</version>
1010

1111
<parent>
12-
<groupId>com.baeldung</groupId>
13-
<artifactId>parent-modules</artifactId>
14-
<version>1.0.0-SNAPSHOT</version>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.5.2.RELEASE</version>
1515
</parent>
1616

1717
<properties>
@@ -21,6 +21,19 @@
2121
</properties>
2222

2323
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-freemarker</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.shiro</groupId>
34+
<artifactId>shiro-spring-boot-web-starter</artifactId>
35+
<version>${apache-shiro-core-version}</version>
36+
</dependency>
2437
<dependency>
2538
<groupId>org.apache.shiro</groupId>
2639
<artifactId>shiro-core</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung;
2+
3+
import org.apache.shiro.realm.Realm;
4+
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
5+
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.annotation.Bean;
11+
12+
/**
13+
* Created by smatt on 21/08/2017.
14+
*/
15+
@SpringBootApplication
16+
public class ShiroSpringApplication {
17+
18+
private static final transient Logger log = LoggerFactory.getLogger(ShiroSpringApplication.class);
19+
20+
public static void main(String... args) {
21+
SpringApplication.run(ShiroSpringApplication.class, args);
22+
}
23+
24+
25+
@Bean
26+
public Realm realm() {
27+
return new MyCustomRealm();
28+
}
29+
30+
31+
@Bean
32+
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
33+
DefaultShiroFilterChainDefinition filter
34+
= new DefaultShiroFilterChainDefinition();
35+
36+
filter.addPathDefinition("/secure", "authc");
37+
filter.addPathDefinition("/**", "anon");
38+
39+
return filter;
40+
}
41+
42+
43+
44+
45+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.controllers;
2+
3+
import com.baeldung.models.UserCredentials;
4+
import org.apache.shiro.SecurityUtils;
5+
import org.apache.shiro.authc.AuthenticationException;
6+
import org.apache.shiro.authc.UsernamePasswordToken;
7+
import org.apache.shiro.subject.Subject;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.ModelMap;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
15+
16+
import javax.servlet.http.HttpServletRequest;
17+
18+
@Controller
19+
public class ShiroSpringController {
20+
21+
22+
23+
@GetMapping("/")
24+
public String index() {
25+
return "index";
26+
}
27+
28+
29+
@RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
30+
public String login(HttpServletRequest req, UserCredentials cred, RedirectAttributes attr) {
31+
32+
if(req.getMethod().equals(RequestMethod.GET.toString())) {
33+
return "login";
34+
}
35+
else {
36+
37+
Subject subject = SecurityUtils.getSubject();
38+
39+
if(!subject.isAuthenticated()) {
40+
UsernamePasswordToken token = new UsernamePasswordToken(
41+
cred.getUsername(), cred.getPassword(), cred.isRememberMe());
42+
try {
43+
subject.login(token);
44+
} catch (AuthenticationException ae) {
45+
ae.printStackTrace();
46+
attr.addFlashAttribute("error", "Invalid Credentials");
47+
return "redirect:/login";
48+
}
49+
}
50+
51+
return "redirect:/secure";
52+
}
53+
}
54+
55+
56+
@GetMapping("/secure")
57+
public String secure(ModelMap modelMap) {
58+
59+
Subject currentUser = SecurityUtils.getSubject();
60+
String role = "", permission = "";
61+
62+
if(currentUser.hasRole("admin")) {
63+
role = role + "You are an Admin";
64+
}
65+
else if(currentUser.hasRole("editor")) {
66+
role = role + "You are an Editor";
67+
}
68+
else if(currentUser.hasRole("author")) {
69+
role = role + "You are an Author";
70+
}
71+
72+
if(currentUser.isPermitted("articles:compose")) {
73+
permission = permission + "You can compose an article, ";
74+
} else {
75+
permission = permission + "You are not permitted to compose an article!, ";
76+
}
77+
78+
if(currentUser.isPermitted("articles:save")) {
79+
permission = permission + "You can save articles, ";
80+
} else {
81+
permission = permission + "\nYou can not save articles, ";
82+
}
83+
84+
if(currentUser.isPermitted("articles:publish")) {
85+
permission = permission + "\nYou can publish articles";
86+
} else {
87+
permission = permission + "\nYou can not publish articles";
88+
}
89+
90+
modelMap.addAttribute("username", currentUser.getPrincipal());
91+
modelMap.addAttribute("permission", permission);
92+
modelMap.addAttribute("role", role);
93+
94+
return "secure";
95+
}
96+
97+
98+
@PostMapping("/logout")
99+
public String logout() {
100+
Subject subject = SecurityUtils.getSubject();
101+
subject.logout();
102+
return "redirect:/";
103+
}
104+
105+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.models;
2+
3+
public class UserCredentials {
4+
5+
private String username;
6+
private String password;
7+
private boolean rememberMe = false;
8+
9+
public UserCredentials() {}
10+
11+
public String getUsername() {
12+
return username;
13+
}
14+
15+
public void setUsername(String username) {
16+
this.username = username;
17+
}
18+
19+
public String getPassword() {
20+
return password;
21+
}
22+
23+
public void setPassword(String password) {
24+
this.password = password;
25+
}
26+
27+
public boolean isRememberMe() {
28+
return rememberMe;
29+
}
30+
31+
public void setRememberMe(boolean rememberMe) {
32+
this.rememberMe = rememberMe;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "username = " + getUsername()
38+
+ "\nrememberMe = " + isRememberMe();
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server.port=9000
2+
server.servlet-path=/
3+
server.context-path=/
4+
5+
#shiro-spring-boot-config
6+
shiro.loginUrl = /login
7+
shiro.successUrl = /secure
8+
shiro.unauthorizedUrl = /login
9+
10+
#freemarker
11+
spring.freemarker.suffix=.ftl
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>Index</title>
4+
</head>
5+
<body>
6+
<h1>Welcome Guest!</h1>
7+
<br>
8+
<a href="/login">Login</a>
9+
</body>
10+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<title>Login</title>
4+
</head>
5+
<body style="margin-left: 30px;">
6+
<h3>Login</h3>
7+
<br>
8+
<form action="/login" method="post">
9+
<#if (error?length > 0)??>
10+
<p style="color:darkred;">${error}</p>
11+
<#else>
12+
</#if>
13+
14+
<label for="username">Username</label>
15+
<br>
16+
<input type="text" name="username">
17+
<br><br>
18+
<label for="password">Password</label>
19+
<br>
20+
<input type="password" name="password">
21+
<br><br>
22+
<input type="checkbox" name="rememberMe"> Remember Me
23+
<br><br>
24+
<input type="submit" value="Submit">
25+
</form>
26+
</body>
27+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<title>Secure</title>
4+
</head>
5+
<body style="margin-left: 30px;">
6+
<h1>Welcome ${username}!</h1>
7+
<p><strong>Role</strong>: ${role}</p>
8+
<p><strong>Permissions</strong></p>
9+
<p>${permission}</p>
10+
<br>
11+
<form role="form" action="/logout" method="POST">
12+
<input type="Submit" value="Logout" />
13+
</form>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)