This commit is contained in:
@@ -10,7 +10,6 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
|
||||
- [How to Determine if a Binary Tree is Balanced](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- [Overview of Combinatorial Problems in Java](https://www.baeldung.com/java-combinatorial-algorithms)
|
||||
- [Prim’s Algorithm](https://www.baeldung.com/java-prim-algorithm)
|
||||
- [Maximum Subarray Problem](https://www.baeldung.com/java-maximum-subarray)
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Articles
|
||||
|
||||
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
||||
@@ -1,5 +0,0 @@
|
||||
## Apache FOP
|
||||
|
||||
This module contains articles about Apache FOP
|
||||
|
||||
### Relevant Articles:
|
||||
@@ -1,4 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [OData Protocol Guide](https://www.baeldung.com/odata)
|
||||
- [Intro to OData with Olingo](https://www.baeldung.com/olingo)
|
||||
|
||||
@@ -8,4 +8,4 @@ This module contains articles about Apache POI
|
||||
- [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow)
|
||||
- [Merge Cells in Excel Using Apache POI](https://www.baeldung.com/java-apache-poi-merge-cells)
|
||||
- [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value)
|
||||
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://github.com/eugenp/tutorials/tree/master/apache-poi)
|
||||
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
|
||||
|
||||
@@ -5,7 +5,7 @@ spring.jpa.hibernate.ddl-auto=create
|
||||
logging.file=azure.log
|
||||
logging.level.root=info
|
||||
|
||||
spring.datasource.url=jdbc:h2:file:~/test
|
||||
spring.datasource.url=jdbc:h2:mem:azure-test-db
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
|
||||
+2
-2
@@ -26,8 +26,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
This module contains articles about Java 8 core features
|
||||
|
||||
### Relevant Articles:
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
- [Run a Java Application from the Command Line](https://www.baeldung.com/java-run-jar-with-arguments)
|
||||
- [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit)
|
||||
|
||||
@@ -48,25 +48,6 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
@@ -74,8 +55,6 @@
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<!-- plugins -->
|
||||
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
## Core Java Arrays (Part 3)
|
||||
|
||||
This module contains articles about Java arrays
|
||||
|
||||
## Relevant Articles
|
||||
@@ -66,101 +66,7 @@
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>libs/</classpathPrefix>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>onejar-maven-plugin</artifactId>
|
||||
<version>${onejar-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
<attachToBuild>true</attachToBuild>
|
||||
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>one-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
@@ -250,110 +156,6 @@
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<!-- java instrumentation profiles to build jars -->
|
||||
<profile>
|
||||
<id>buildAgentLoader</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agentLoader</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/AgentLoader.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildApplication</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>application</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildAgent</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agent</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
@@ -368,10 +170,6 @@
|
||||
|
||||
<!-- maven and spring plugins -->
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
||||
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
||||
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class PrimeNumbersUnitTest {
|
||||
public class PrimeNumbersUnitManualTest {
|
||||
|
||||
private static Logger logger = Logger.getAnonymousLogger();
|
||||
|
||||
@@ -11,5 +11,4 @@ This module contains articles about Java operators
|
||||
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators)
|
||||
- [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator)
|
||||
- [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators)
|
||||
- [Bitwise & vs Logical && Operators](https://www.baeldung.com/bitwise-vs-logical-operators/)
|
||||
|
||||
- [Bitwise & vs Logical && Operators](https://www.baeldung.com/java-bitwise-vs-logical-and)
|
||||
|
||||
@@ -25,6 +25,12 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.15.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.replacetokens;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ReplacingTokens {
|
||||
public static final Pattern TITLE_CASE_PATTERN = Pattern.compile("(?<=^|[^A-Za-z])([A-Z][a-z]*)(?=[^A-Za-z]|$)");
|
||||
|
||||
/**
|
||||
* Iterate over the title case tokens in the input and replace them with lowercase
|
||||
* @param original the original string
|
||||
* @return a string with words replaced with their lowercase equivalents
|
||||
*/
|
||||
public static String replaceTitleCaseWithLowerCase(String original) {
|
||||
int lastIndex = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
Matcher matcher = TITLE_CASE_PATTERN.matcher(original);
|
||||
while (matcher.find()) {
|
||||
output.append(original, lastIndex, matcher.start())
|
||||
.append(convert(matcher.group(1)));
|
||||
|
||||
lastIndex = matcher.end();
|
||||
}
|
||||
if (lastIndex < original.length()) {
|
||||
output.append(original, lastIndex, original.length());
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a token found into its desired lowercase
|
||||
* @param token the token to convert
|
||||
* @return the converted token
|
||||
*/
|
||||
private static String convert(String token) {
|
||||
return token.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all the tokens in an input using the algorithm provided for each
|
||||
* @param original original string
|
||||
* @param tokenPattern the pattern to match with
|
||||
* @param converter the conversion to apply
|
||||
* @return the substituted string
|
||||
*/
|
||||
public static String replaceTokens(String original, Pattern tokenPattern,
|
||||
Function<Matcher, String> converter) {
|
||||
int lastIndex = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
Matcher matcher = tokenPattern.matcher(original);
|
||||
while (matcher.find()) {
|
||||
output.append(original, lastIndex, matcher.start())
|
||||
.append(converter.apply(matcher));
|
||||
|
||||
lastIndex = matcher.end();
|
||||
}
|
||||
if (lastIndex < original.length()) {
|
||||
output.append(original, lastIndex, original.length());
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.replacetokens;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.baeldung.replacetokens.ReplacingTokens.TITLE_CASE_PATTERN;
|
||||
import static com.baeldung.replacetokens.ReplacingTokens.replaceTokens;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ReplacingTokensUnitTest {
|
||||
private static final String EXAMPLE_INPUT = "First 3 Capital Words! then 10 TLAs, I Found";
|
||||
private static final String EXAMPLE_INPUT_PROCESSED = "first 3 capital words! then 10 TLAs, i found";
|
||||
|
||||
@Test
|
||||
public void whenFindMatches_thenTitleWordsFound() {
|
||||
Matcher matcher = TITLE_CASE_PATTERN.matcher(EXAMPLE_INPUT);
|
||||
List<String> matches = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
matches.add(matcher.group(1));
|
||||
}
|
||||
|
||||
assertThat(matches)
|
||||
.containsExactly("First", "Capital", "Words", "I", "Found");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exploreMatches() {
|
||||
Matcher matcher = TITLE_CASE_PATTERN.matcher(EXAMPLE_INPUT);
|
||||
while (matcher.find()) {
|
||||
System.out.println("Match: " + matcher.group(0));
|
||||
System.out.println("Start: " + matcher.start());
|
||||
System.out.println("End: " + matcher.end());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceTokensWithLowerCase() {
|
||||
assertThat(ReplacingTokens.replaceTitleCaseWithLowerCase(EXAMPLE_INPUT))
|
||||
.isEqualTo(EXAMPLE_INPUT_PROCESSED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceTokensWithLowerCaseUsingGeneralPurpose() {
|
||||
assertThat(replaceTokens("First 3 Capital Words! then 10 TLAs, I Found",
|
||||
TITLE_CASE_PATTERN,
|
||||
match -> match.group(1).toLowerCase()))
|
||||
.isEqualTo("first 3 capital words! then 10 TLAs, i found");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void escapeRegexCharacters() {
|
||||
Pattern regexCharacters = Pattern.compile("[<(\\[{\\\\^\\-=$!|\\]})?*+.>]");
|
||||
|
||||
assertThat(replaceTokens("A regex character like [",
|
||||
regexCharacters,
|
||||
match -> "\\" + match.group()))
|
||||
.isEqualTo("A regex character like \\[");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replacePlaceholders() {
|
||||
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<placeholder>[A-Za-z0-9-_]+)}");
|
||||
|
||||
Map<String, String> placeholderValues = new HashMap<>();
|
||||
placeholderValues.put("name", "Bill");
|
||||
placeholderValues.put("company", "Baeldung");
|
||||
|
||||
assertThat(replaceTokens("Hi ${name} at ${company}",
|
||||
placeholderPattern,
|
||||
match -> placeholderValues.get(match.group("placeholder"))))
|
||||
.isEqualTo("Hi Bill at Baeldung");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-security-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-security-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.callback.*;
|
||||
import java.io.Console;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConsoleCallbackHandler implements CallbackHandler {
|
||||
|
||||
@Override
|
||||
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
|
||||
Console console = System.console();
|
||||
for (Callback callback : callbacks) {
|
||||
if (callback instanceof NameCallback) {
|
||||
NameCallback nameCallback = (NameCallback) callback;
|
||||
nameCallback.setName(console.readLine(nameCallback.getPrompt()));
|
||||
} else if (callback instanceof PasswordCallback) {
|
||||
PasswordCallback passwordCallback = (PasswordCallback) callback;
|
||||
passwordCallback.setPassword(console.readPassword(passwordCallback.getPrompt()));
|
||||
} else {
|
||||
throw new UnsupportedCallbackException(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
public class JaasAuthentication {
|
||||
|
||||
public static void main(String[] args) throws LoginException {
|
||||
LoginService loginService = new LoginService();
|
||||
Subject subject = loginService.login();
|
||||
System.out.println(subject.getPrincipals().iterator().next() + " sucessfully logeed in");
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
public class JaasAuthorization {
|
||||
|
||||
public static void main(String[] args) throws LoginException {
|
||||
|
||||
LoginService loginService = new LoginService();
|
||||
Subject subject = loginService.login();
|
||||
|
||||
PrivilegedAction privilegedAction = new ResourceAction();
|
||||
Subject.doAsPrivileged(subject, privilegedAction, null);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.login.LoginContext;
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
public class LoginService {
|
||||
|
||||
public Subject login() throws LoginException {
|
||||
LoginContext loginContext = new LoginContext("jaasApplication", new ConsoleCallbackHandler());
|
||||
loginContext.login();
|
||||
return loginContext.getSubject();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
public class ResourceAction implements PrivilegedAction {
|
||||
@Override
|
||||
public Object run() {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null) {
|
||||
sm.checkPermission(new ResourcePermission("test_resource"));
|
||||
}
|
||||
System.out.println("I have access to test_resource !");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.jaas;
|
||||
|
||||
import java.security.BasicPermission;
|
||||
|
||||
public class ResourcePermission extends BasicPermission {
|
||||
public ResourcePermission(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.jaas.loginmodule;
|
||||
|
||||
import com.sun.security.auth.UserPrincipal;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
import javax.security.auth.callback.*;
|
||||
import javax.security.auth.login.LoginException;
|
||||
import javax.security.auth.spi.LoginModule;
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
public class InMemoryLoginModule implements LoginModule {
|
||||
|
||||
private static final String USERNAME = "testuser";
|
||||
private static final String PASSWORD = "testpassword";
|
||||
|
||||
private Subject subject;
|
||||
private CallbackHandler callbackHandler;
|
||||
private Map<String, ?> sharedState;
|
||||
private Map<String, ?> options;
|
||||
|
||||
private String username;
|
||||
private boolean loginSucceeded = false;
|
||||
private Principal userPrincipal;
|
||||
|
||||
@Override
|
||||
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
|
||||
Map<String, ?> options) {
|
||||
this.subject = subject;
|
||||
this.callbackHandler = callbackHandler;
|
||||
this.sharedState = sharedState;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean login() throws LoginException {
|
||||
NameCallback nameCallback = new NameCallback("username: ");
|
||||
PasswordCallback passwordCallback = new PasswordCallback("password: ", false);
|
||||
try {
|
||||
callbackHandler.handle(new Callback[]{nameCallback, passwordCallback});
|
||||
username = nameCallback.getName();
|
||||
String password = new String(passwordCallback.getPassword());
|
||||
if (USERNAME.equals(username) && PASSWORD.equals(password)) {
|
||||
loginSucceeded = true;
|
||||
}
|
||||
} catch (IOException | UnsupportedCallbackException e) {
|
||||
throw new LoginException("Can't login");
|
||||
}
|
||||
return loginSucceeded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commit() throws LoginException {
|
||||
if (!loginSucceeded) {
|
||||
return false;
|
||||
}
|
||||
userPrincipal = new UserPrincipal(username);
|
||||
subject.getPrincipals().add(userPrincipal);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean abort() throws LoginException {
|
||||
logout();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean logout() throws LoginException {
|
||||
subject.getPrincipals().remove(userPrincipal);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
jaasApplication {
|
||||
com.baeldung.jaas.loginmodule.InMemoryLoginModule required debug=true;
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
grant codebase "file:./target/core-java-security-2-0.1.0-SNAPSHOT.jar" {
|
||||
permission javax.security.auth.AuthPermission "createLoginContext.jaasApplication";
|
||||
permission javax.security.auth.AuthPermission "doAsPrivileged";
|
||||
permission java.lang.RuntimePermission "readFileDescriptor";
|
||||
permission java.lang.RuntimePermission "writeFileDescriptor";
|
||||
};
|
||||
|
||||
grant codebase "file:./target/core-java-security-2-0.1.0-SNAPSHOT.jar" {
|
||||
permission javax.security.auth.AuthPermission "modifyPrincipals";
|
||||
};
|
||||
|
||||
grant principal com.sun.security.auth.UserPrincipal "testuser" {
|
||||
permission com.baeldung.jaas.ResourcePermission "test_resource";
|
||||
};
|
||||
@@ -28,8 +28,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -44,8 +44,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -48,8 +48,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -40,20 +40,6 @@
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>libs/</classpathPrefix>
|
||||
<mainClass>com.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
@@ -115,7 +101,6 @@
|
||||
|
||||
<!-- maven plugins -->
|
||||
<sun-tools.version>1.8.0</sun-tools.version>
|
||||
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user