Merge remote-tracking branch 'upstream/master' into BAEL_4302
This commit is contained in:
+10
-1
@@ -39,10 +39,19 @@
|
|||||||
<artifactId>jcl-over-slf4j</artifactId>
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- spring-sec -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<apache-shiro-core-version>1.4.0</apache-shiro-core-version>
|
<apache-shiro-core-version>1.5.3</apache-shiro-core-version>
|
||||||
<log4j-version>1.2.17</log4j-version>
|
<log4j-version>1.2.17</log4j-version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package com.baeldung.comparison.shiro;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
|
import org.apache.shiro.authc.AuthenticationInfo;
|
||||||
|
import org.apache.shiro.authc.AuthenticationToken;
|
||||||
|
import org.apache.shiro.authc.SimpleAuthenticationInfo;
|
||||||
|
import org.apache.shiro.authc.UnknownAccountException;
|
||||||
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||||
|
import org.apache.shiro.authz.AuthorizationInfo;
|
||||||
|
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
||||||
|
import org.apache.shiro.realm.jdbc.JdbcRealm;
|
||||||
|
import org.apache.shiro.subject.PrincipalCollection;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class CustomRealm extends JdbcRealm {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(CustomRealm.class);
|
||||||
|
|
||||||
|
private Map<String, String> credentials = new HashMap<>();
|
||||||
|
private Map<String, Set<String>> roles = new HashMap<>();
|
||||||
|
private Map<String, Set<String>> permissions = new HashMap<>();
|
||||||
|
|
||||||
|
{
|
||||||
|
credentials.put("Tom", "password");
|
||||||
|
credentials.put("Jerry", "password");
|
||||||
|
|
||||||
|
roles.put("Jerry", new HashSet<>(Arrays.asList("ADMIN")));
|
||||||
|
roles.put("Tom", new HashSet<>(Arrays.asList("USER")));
|
||||||
|
|
||||||
|
permissions.put("ADMIN", new HashSet<>(Arrays.asList("READ", "WRITE")));
|
||||||
|
permissions.put("USER", new HashSet<>(Arrays.asList("READ")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
|
||||||
|
|
||||||
|
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
|
||||||
|
|
||||||
|
if (userToken.getUsername() == null || userToken.getUsername()
|
||||||
|
.isEmpty() || !credentials.containsKey(userToken.getUsername())) {
|
||||||
|
throw new UnknownAccountException("User doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SimpleAuthenticationInfo(userToken.getUsername(), credentials.get(userToken.getUsername()), getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
||||||
|
Set<String> roles = new HashSet<>();
|
||||||
|
Set<String> permissions = new HashSet<>();
|
||||||
|
|
||||||
|
for (Object user : principals) {
|
||||||
|
try {
|
||||||
|
roles.addAll(getRoleNamesForUser(null, (String) user));
|
||||||
|
permissions.addAll(getPermissions(null, null, roles));
|
||||||
|
} catch (SQLException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo(roles);
|
||||||
|
authInfo.setStringPermissions(permissions);
|
||||||
|
return authInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
|
||||||
|
if (!roles.containsKey(username)) {
|
||||||
|
throw new SQLException("User doesn't exist");
|
||||||
|
}
|
||||||
|
return roles.get(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Set<String> getPermissions(Connection conn, String username, Collection<String> roles) throws SQLException {
|
||||||
|
Set<String> userPermissions = new HashSet<>();
|
||||||
|
|
||||||
|
for (String role : roles) {
|
||||||
|
if (!permissions.containsKey(role)) {
|
||||||
|
throw new SQLException("Role doesn't exist");
|
||||||
|
}
|
||||||
|
userPermissions.addAll(permissions.get(role));
|
||||||
|
}
|
||||||
|
return userPermissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.comparison.shiro;
|
||||||
|
|
||||||
|
import org.apache.shiro.realm.Realm;
|
||||||
|
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
|
||||||
|
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||||
|
public class ShiroApplication {
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
SpringApplication.run(ShiroApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Realm customRealm() {
|
||||||
|
return new CustomRealm();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
|
||||||
|
DefaultShiroFilterChainDefinition filter = new DefaultShiroFilterChainDefinition();
|
||||||
|
|
||||||
|
filter.addPathDefinition("/home", "authc");
|
||||||
|
filter.addPathDefinition("/**", "anon");
|
||||||
|
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+99
@@ -0,0 +1,99 @@
|
|||||||
|
package com.baeldung.comparison.shiro.controllers;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
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.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.baeldung.comparison.shiro.models.UserCredentials;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class ShiroController {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(ShiroController.class);
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public String getIndex() {
|
||||||
|
return "comparison/index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/login")
|
||||||
|
public String showLoginPage() {
|
||||||
|
return "comparison/login";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public String doLogin(HttpServletRequest req, UserCredentials credentials, RedirectAttributes attr) {
|
||||||
|
|
||||||
|
Subject subject = SecurityUtils.getSubject();
|
||||||
|
|
||||||
|
if (!subject.isAuthenticated()) {
|
||||||
|
UsernamePasswordToken token = new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword());
|
||||||
|
try {
|
||||||
|
subject.login(token);
|
||||||
|
} catch (AuthenticationException ae) {
|
||||||
|
logger.error(ae.getMessage());
|
||||||
|
attr.addFlashAttribute("error", "Invalid Credentials");
|
||||||
|
return "redirect:/login";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "redirect:/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/home")
|
||||||
|
public String getMeHome(Model model) {
|
||||||
|
|
||||||
|
addUserAttributes(model);
|
||||||
|
|
||||||
|
return "comparison/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/admin")
|
||||||
|
public String adminOnly(Model model) {
|
||||||
|
addUserAttributes(model);
|
||||||
|
|
||||||
|
Subject currentUser = SecurityUtils.getSubject();
|
||||||
|
if (currentUser.hasRole("ADMIN")) {
|
||||||
|
model.addAttribute("adminContent", "only admin can view this");
|
||||||
|
}
|
||||||
|
return "comparison/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/logout")
|
||||||
|
public String logout() {
|
||||||
|
Subject subject = SecurityUtils.getSubject();
|
||||||
|
subject.logout();
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addUserAttributes(Model model) {
|
||||||
|
Subject currentUser = SecurityUtils.getSubject();
|
||||||
|
String permission = "";
|
||||||
|
|
||||||
|
if (currentUser.hasRole("ADMIN")) {
|
||||||
|
model.addAttribute("role", "ADMIN");
|
||||||
|
} else if (currentUser.hasRole("USER")) {
|
||||||
|
model.addAttribute("role", "USER");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentUser.isPermitted("READ")) {
|
||||||
|
permission = permission + " READ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentUser.isPermitted("WRITE")) {
|
||||||
|
permission = permission + " WRITE";
|
||||||
|
}
|
||||||
|
model.addAttribute("username", currentUser.getPrincipal());
|
||||||
|
model.addAttribute("permission", permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.comparison.shiro.models;
|
||||||
|
|
||||||
|
public class UserCredentials {
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "username = " + getUsername();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.comparison.springsecurity;
|
||||||
|
|
||||||
|
import org.apache.shiro.spring.boot.autoconfigure.ShiroAnnotationProcessorAutoConfiguration;
|
||||||
|
import org.apache.shiro.spring.boot.autoconfigure.ShiroAutoConfiguration;
|
||||||
|
import org.apache.shiro.spring.config.web.autoconfigure.ShiroWebAutoConfiguration;
|
||||||
|
import org.apache.shiro.spring.config.web.autoconfigure.ShiroWebFilterConfiguration;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = {ShiroAutoConfiguration.class,
|
||||||
|
ShiroAnnotationProcessorAutoConfiguration.class,
|
||||||
|
ShiroWebAutoConfiguration.class,
|
||||||
|
ShiroWebFilterConfiguration.class})
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.comparison.springsecurity.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http.csrf().disable().authorizeRequests(authorize -> authorize.antMatchers("/index", "/login")
|
||||||
|
.permitAll()
|
||||||
|
.antMatchers("/home", "/logout")
|
||||||
|
.authenticated()
|
||||||
|
.antMatchers("/admin/**")
|
||||||
|
.hasRole("ADMIN"))
|
||||||
|
.formLogin(formLogin -> formLogin.loginPage("/login")
|
||||||
|
.failureUrl("/login-error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
auth.inMemoryAuthentication()
|
||||||
|
.withUser("Jerry")
|
||||||
|
.password(passwordEncoder().encode("password"))
|
||||||
|
.authorities("READ", "WRITE")
|
||||||
|
.roles("ADMIN")
|
||||||
|
.and()
|
||||||
|
.withUser("Tom")
|
||||||
|
.password(passwordEncoder().encode("password"))
|
||||||
|
.authorities("READ")
|
||||||
|
.roles("USER");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
package com.baeldung.comparison.springsecurity.web;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class SpringController {
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public String getIndex() {
|
||||||
|
return "comparison/index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/login")
|
||||||
|
public String showLoginPage() {
|
||||||
|
return "comparison/login";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/login-error")
|
||||||
|
public String loginError(Model model) {
|
||||||
|
model.addAttribute("error", "Invalid Credentials");
|
||||||
|
return "comparison/login";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public String doLogin(HttpServletRequest req) {
|
||||||
|
return "redirect:/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/home")
|
||||||
|
public String showHomePage(HttpServletRequest req, Model model) {
|
||||||
|
addUserAttributes(model);
|
||||||
|
return "comparison/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/admin")
|
||||||
|
public String adminOnly(HttpServletRequest req, Model model) {
|
||||||
|
addUserAttributes(model);
|
||||||
|
model.addAttribute("adminContent", "only admin can view this");
|
||||||
|
return "comparison/home";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addUserAttributes(Model model) {
|
||||||
|
Authentication auth = SecurityContextHolder.getContext()
|
||||||
|
.getAuthentication();
|
||||||
|
if (auth != null && !auth.getClass()
|
||||||
|
.equals(AnonymousAuthenticationToken.class)) {
|
||||||
|
User user = (User) auth.getPrincipal();
|
||||||
|
model.addAttribute("username", user.getUsername());
|
||||||
|
|
||||||
|
Collection<GrantedAuthority> authorities = user.getAuthorities();
|
||||||
|
|
||||||
|
for (GrantedAuthority authority : authorities) {
|
||||||
|
if (authority.getAuthority()
|
||||||
|
.contains("USER")) {
|
||||||
|
model.addAttribute("role", "USER");
|
||||||
|
model.addAttribute("permission", "READ");
|
||||||
|
} else if (authority.getAuthority()
|
||||||
|
.contains("ADMIN")) {
|
||||||
|
model.addAttribute("role", "ADMIN");
|
||||||
|
model.addAttribute("permission", "READ WRITE");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung;
|
package com.baeldung.intro;
|
||||||
|
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung;
|
package com.baeldung.intro;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
+4
-3
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung;
|
package com.baeldung.intro;
|
||||||
|
|
||||||
import org.apache.shiro.realm.Realm;
|
import org.apache.shiro.realm.Realm;
|
||||||
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
|
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
|
||||||
@@ -7,12 +7,13 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by smatt on 21/08/2017.
|
* Created by smatt on 21/08/2017.
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||||
public class ShiroSpringApplication {
|
public class ShiroSpringApplication {
|
||||||
|
|
||||||
private static final transient Logger log = LoggerFactory.getLogger(ShiroSpringApplication.class);
|
private static final transient Logger log = LoggerFactory.getLogger(ShiroSpringApplication.class);
|
||||||
@@ -29,7 +30,7 @@ public class ShiroSpringApplication {
|
|||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
|
public ShiroFilterChainDefinition filterChainDefinition() {
|
||||||
DefaultShiroFilterChainDefinition filter
|
DefaultShiroFilterChainDefinition filter
|
||||||
= new DefaultShiroFilterChainDefinition();
|
= new DefaultShiroFilterChainDefinition();
|
||||||
|
|
||||||
+3
-2
@@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.controllers;
|
package com.baeldung.intro.controllers;
|
||||||
|
|
||||||
import com.baeldung.models.UserCredentials;
|
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||||
@@ -13,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.baeldung.intro.models.UserCredentials;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.models;
|
package com.baeldung.intro.models;
|
||||||
|
|
||||||
public class UserCredentials {
|
public class UserCredentials {
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.shiro.permissions.custom;
|
package com.baeldung.permissions.custom;
|
||||||
|
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.shiro.permissions.custom;
|
package com.baeldung.permissions.custom;
|
||||||
|
|
||||||
import org.apache.shiro.authz.Permission;
|
import org.apache.shiro.authz.Permission;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.shiro.permissions.custom;
|
package com.baeldung.permissions.custom;
|
||||||
|
|
||||||
import org.apache.shiro.authz.Permission;
|
import org.apache.shiro.authz.Permission;
|
||||||
import org.apache.shiro.authz.permission.PermissionResolver;
|
import org.apache.shiro.authz.permission.PermissionResolver;
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Home Page</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>
|
||||||
|
<a href="/admin">Admin only</a>
|
||||||
|
<#if adminContent??>
|
||||||
|
${adminContent}
|
||||||
|
</#if>
|
||||||
|
<br>
|
||||||
|
<form role="form" action="/logout" method="POST">
|
||||||
|
<input type="Submit" value="Logout" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Index</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome Guest!</h1>
|
||||||
|
<br>
|
||||||
|
Go to the <a href="/home">secured page
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<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="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.comparison.shiro;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import com.baeldung.comparison.shiro.ShiroApplication;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(classes = { ShiroApplication.class })
|
||||||
|
public class SpringContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.comparison.springsecurity;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import com.baeldung.comparison.springsecurity.Application;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(classes = { Application.class })
|
||||||
|
public class SpringContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -46,7 +46,4 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -27,6 +27,11 @@
|
|||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.10</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.collections.iterators;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import com.google.common.primitives.Ints;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
|
||||||
|
public class ConvertPrimitivesArrayToList {
|
||||||
|
|
||||||
|
public static void failConvert() {
|
||||||
|
int[] input = new int[]{1,2,3,4};
|
||||||
|
// List<Integer> inputAsList = Arrays.asList(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> iterateConvert(int[] input) {
|
||||||
|
List<Integer> output = new ArrayList<Integer>();
|
||||||
|
for (int value : input) {
|
||||||
|
output.add(value);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> streamConvert(int[] input) {
|
||||||
|
List<Integer> output = Arrays.stream(input).boxed().collect(Collectors.toList());
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> streamConvertIntStream(int[] input) {
|
||||||
|
List<Integer> output = IntStream.of(input).boxed().collect(Collectors.toList());
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> guavaConvert(int[] input) {
|
||||||
|
List<Integer> output = Ints.asList(input);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> apacheCommonConvert(int[] input) {
|
||||||
|
Integer[] outputBoxed = ArrayUtils.toObject(input);
|
||||||
|
List<Integer> output = Arrays.asList(outputBoxed);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.collections.iterators;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import com.google.common.primitives.Ints;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class ConvertPrimitivesArrayToListUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayWithPrimitives_whenIterativeConvert_thenArrayGetsConverted() {
|
||||||
|
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.iterateConvert(new int[]{1,2,3,4}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayWithPrimitives_whenStreamConvert_thenArrayGetsConverted() {
|
||||||
|
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvert(new int[]{1,2,3,4}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayWithPrimitives_whenIntStreamConvert_thenArrayGetsConverted() {
|
||||||
|
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.streamConvertIntStream(new int[]{1,2,3,4}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayWithPrimitives_whenGuavaConvert_thenArrayGetsConverted() {
|
||||||
|
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.guavaConvert(new int[]{1,2,3,4}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayWithPrimitives_whenApacheCommonConvert_thenArrayGetsConverted() {
|
||||||
|
assertEquals(Arrays.asList(1,2,3,4), ConvertPrimitivesArrayToList.apacheCommonConvert(new int[]{1,2,3,4}));
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.concurrent.localvariables;
|
||||||
|
|
||||||
|
public class LocalAndLambda {
|
||||||
|
public static void main(String... args) {
|
||||||
|
String text = "";
|
||||||
|
// Un-commenting the next line will break compilation, because text is no longer effectively final
|
||||||
|
// text = "675";
|
||||||
|
new Thread(() -> System.out.println(text)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.concurrent.localvariables;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
public class LocalVariables implements Runnable {
|
||||||
|
private int field;
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
LocalVariables target = new LocalVariables();
|
||||||
|
new Thread(target).start();
|
||||||
|
new Thread(target).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
field = new SecureRandom().nextInt();
|
||||||
|
int local = new SecureRandom().nextInt();
|
||||||
|
System.out.println(field + " - " + local);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Introduction to Lock Striping](https://www.baeldung.com/java-lock-stripping)
|
- [Introduction to Lock Striping](https://www.baeldung.com/java-lock-stripping)
|
||||||
|
- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue)
|
||||||
|
- [[<-- Prev]](/core-java-modules/core-java-concurrency-collections)
|
||||||
|
|||||||
@@ -3,9 +3,16 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.concurrent.lock</groupId>
|
|
||||||
<artifactId>core-java-concurrency-collections-2</artifactId>
|
<artifactId>core-java-concurrency-collections-2</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-concurrency-collections-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -30,19 +37,6 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
|
||||||
<sourceDirectory>src</sourceDirectory>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>3.8.0</version>
|
|
||||||
<configuration>
|
|
||||||
<source>1.8</source>
|
|
||||||
<target>1.8</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jmh.version>1.21</jmh.version>
|
<jmh.version>1.21</jmh.version>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user