Updated Example Code for Apache Shiro (#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
This commit is contained in:
parent
8180274da2
commit
cec17bdbfe
@ -9,9 +9,9 @@
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
@ -21,6 +21,19 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-spring-boot-web-starter</artifactId>
|
||||
<version>${apache-shiro-core-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-core</artifactId>
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.apache.shiro.realm.Realm;
|
||||
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
|
||||
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Created by smatt on 21/08/2017.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ShiroSpringApplication {
|
||||
|
||||
private static final transient Logger log = LoggerFactory.getLogger(ShiroSpringApplication.class);
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(ShiroSpringApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public Realm realm() {
|
||||
return new MyCustomRealm();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
|
||||
DefaultShiroFilterChainDefinition filter
|
||||
= new DefaultShiroFilterChainDefinition();
|
||||
|
||||
filter.addPathDefinition("/secure", "authc");
|
||||
filter.addPathDefinition("/**", "anon");
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.models.UserCredentials;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Controller
|
||||
public class ShiroSpringController {
|
||||
|
||||
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
|
||||
public String login(HttpServletRequest req, UserCredentials cred, RedirectAttributes attr) {
|
||||
|
||||
if(req.getMethod().equals(RequestMethod.GET.toString())) {
|
||||
return "login";
|
||||
}
|
||||
else {
|
||||
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
|
||||
if(!subject.isAuthenticated()) {
|
||||
UsernamePasswordToken token = new UsernamePasswordToken(
|
||||
cred.getUsername(), cred.getPassword(), cred.isRememberMe());
|
||||
try {
|
||||
subject.login(token);
|
||||
} catch (AuthenticationException ae) {
|
||||
ae.printStackTrace();
|
||||
attr.addFlashAttribute("error", "Invalid Credentials");
|
||||
return "redirect:/login";
|
||||
}
|
||||
}
|
||||
|
||||
return "redirect:/secure";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/secure")
|
||||
public String secure(ModelMap modelMap) {
|
||||
|
||||
Subject currentUser = SecurityUtils.getSubject();
|
||||
String role = "", permission = "";
|
||||
|
||||
if(currentUser.hasRole("admin")) {
|
||||
role = role + "You are an Admin";
|
||||
}
|
||||
else if(currentUser.hasRole("editor")) {
|
||||
role = role + "You are an Editor";
|
||||
}
|
||||
else if(currentUser.hasRole("author")) {
|
||||
role = role + "You are an Author";
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:compose")) {
|
||||
permission = permission + "You can compose an article, ";
|
||||
} else {
|
||||
permission = permission + "You are not permitted to compose an article!, ";
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:save")) {
|
||||
permission = permission + "You can save articles, ";
|
||||
} else {
|
||||
permission = permission + "\nYou can not save articles, ";
|
||||
}
|
||||
|
||||
if(currentUser.isPermitted("articles:publish")) {
|
||||
permission = permission + "\nYou can publish articles";
|
||||
} else {
|
||||
permission = permission + "\nYou can not publish articles";
|
||||
}
|
||||
|
||||
modelMap.addAttribute("username", currentUser.getPrincipal());
|
||||
modelMap.addAttribute("permission", permission);
|
||||
modelMap.addAttribute("role", role);
|
||||
|
||||
return "secure";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/logout")
|
||||
public String logout() {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
subject.logout();
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.baeldung.models;
|
||||
|
||||
public class UserCredentials {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean rememberMe = false;
|
||||
|
||||
public UserCredentials() {}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public boolean isRememberMe() {
|
||||
return rememberMe;
|
||||
}
|
||||
|
||||
public void setRememberMe(boolean rememberMe) {
|
||||
this.rememberMe = rememberMe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "username = " + getUsername()
|
||||
+ "\nrememberMe = " + isRememberMe();
|
||||
}
|
||||
}
|
11
apache-shiro/src/main/resources/application.properties
Normal file
11
apache-shiro/src/main/resources/application.properties
Normal file
@ -0,0 +1,11 @@
|
||||
server.port=9000
|
||||
server.servlet-path=/
|
||||
server.context-path=/
|
||||
|
||||
#shiro-spring-boot-config
|
||||
shiro.loginUrl = /login
|
||||
shiro.successUrl = /secure
|
||||
shiro.unauthorizedUrl = /login
|
||||
|
||||
#freemarker
|
||||
spring.freemarker.suffix=.ftl
|
10
apache-shiro/src/main/resources/templates/index.ftl
Normal file
10
apache-shiro/src/main/resources/templates/index.ftl
Normal file
@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Index</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome Guest!</h1>
|
||||
<br>
|
||||
<a href="/login">Login</a>
|
||||
</body>
|
||||
</html>
|
27
apache-shiro/src/main/resources/templates/login.ftl
Normal file
27
apache-shiro/src/main/resources/templates/login.ftl
Normal file
@ -0,0 +1,27 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body style="margin-left: 30px;">
|
||||
<h3>Login</h3>
|
||||
<br>
|
||||
<form action="/login" method="post">
|
||||
<#if (error?length > 0)??>
|
||||
<p style="color:darkred;">${error}</p>
|
||||
<#else>
|
||||
</#if>
|
||||
|
||||
<label for="username">Username</label>
|
||||
<br>
|
||||
<input type="text" name="username">
|
||||
<br><br>
|
||||
<label for="password">Password</label>
|
||||
<br>
|
||||
<input type="password" name="password">
|
||||
<br><br>
|
||||
<input type="checkbox" name="rememberMe"> Remember Me
|
||||
<br><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
15
apache-shiro/src/main/resources/templates/secure.ftl
Normal file
15
apache-shiro/src/main/resources/templates/secure.ftl
Normal file
@ -0,0 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Secure</title>
|
||||
</head>
|
||||
<body style="margin-left: 30px;">
|
||||
<h1>Welcome ${username}!</h1>
|
||||
<p><strong>Role</strong>: ${role}</p>
|
||||
<p><strong>Permissions</strong></p>
|
||||
<p>${permission}</p>
|
||||
<br>
|
||||
<form role="form" action="/logout" method="POST">
|
||||
<input type="Submit" value="Logout" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user