Skip to content

Commit

Permalink
Interface with NetLicensing / validate() customer
Browse files Browse the repository at this point in the history
  • Loading branch information
r-brown committed Nov 12, 2020
1 parent 297c684 commit 3866ba6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 23 deletions.
23 changes: 23 additions & 0 deletions complete/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@
</properties>

<dependencies>
<dependency>
<groupId>com.labs64.netlicensing</groupId>
<artifactId>netlicensing-client</artifactId>
<version>2.5.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.securingweb;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.labs64.netlicensing.domain.vo.Composition;
import com.labs64.netlicensing.domain.vo.Context;
import com.labs64.netlicensing.domain.vo.SecurityMode;
import com.labs64.netlicensing.domain.vo.ValidationParameters;
import com.labs64.netlicensing.domain.vo.ValidationResult;
import com.labs64.netlicensing.exception.NetLicensingException;
import com.labs64.netlicensing.service.LicenseeService;

@Controller
public class NetLicensingController {

@GetMapping("/hello")
public String greeting(Model model) throws NetLicensingException {

// NetLicensing configuration
// API Key - min role needed: Licensee
final String NLIC_APIKEY = "ea624604-028e-458c-b5e5-85bc6620b890";
// Product number
final String NLC_PRODUCT = "P1S85TDRU-DEMO";
// Product module number
final String NLC_MODULE = "MTGTOW8EC-DEMO";

// Initiate NetLicensing context
final Context context = new Context();
context.setBaseUrl("https://go.netlicensing.io/core/v2/rest");
context.setSecurityMode(SecurityMode.APIKEY_IDENTIFICATION);
context.setApiKey(NLIC_APIKEY);

// Prepare validation request
final ValidationParameters validationParameters = new ValidationParameters();
validationParameters.setProductNumber(NLC_PRODUCT);
// Send validation request; where current login username will be used as unique customer identifier
ValidationResult validationResult = LicenseeService.validate(context, getUsername(), validationParameters);

// Prepare model to be rendered on the hello page
Composition moduleValidation = validationResult.getProductModuleValidation(NLC_MODULE);
model.addAttribute("name", getUsername());
model.addAttribute("moduleValidation", moduleValidation);

return "hello";
}

/**
* Extract logged-in username.
*/
private String getUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
return currentUserName;
}
return "unknown";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,28 @@
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated().and().formLogin()
.loginPage("/login").permitAll().and().logout().permitAll();
}

@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new UserDetailsService() {

return new InMemoryUserDetailsManager(user);
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Set password same as username
return User.withDefaultPasswordEncoder().username(username).password(username).roles("USER").build();
}

};
}
}
8 changes: 7 additions & 1 deletion complete/src/main/resources/templates/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
<title>Hello NetLicensing!</title>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<hr>
<h2 th:inline="text">[[${moduleValidation.properties.get('productModuleName')}]]!</h2>
<p th:text="'- valid: ' + ${moduleValidation.properties['valid']}" />
<p th:text="'- expires: ' + ${moduleValidation.properties['expires']}" />
<p th:text="'RAW validation result: ' + ${moduleValidation}" />
<hr>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Sign Out"/>
</form>
Expand Down
4 changes: 2 additions & 2 deletions complete/src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1>
<h1>Welcome to NetLicensing / Spring Security!</h1>

<p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
<p>Click <a th:href="@{/hello}">here</a> to see a customer's validation status.</p>
</body>
</html>
3 changes: 2 additions & 1 deletion complete/src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><label> Password : <input type="password" name="password"/> </label></div>
<span><i>(provide password same as username)</i></span>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
Expand Down

0 comments on commit 3866ba6

Please sign in to comment.