forked from frankframework/frankframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Bus Security over Gateways (frankframework#5286)
- Loading branch information
Showing
13 changed files
with
782 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
console/war/src/main/java/nl/nn/adapterframework/web/JwksEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2023 WeAreFrank! | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package nl.nn.adapterframework.web; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.boot.web.servlet.ServletRegistrationBean; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import lombok.Setter; | ||
import nl.nn.adapterframework.management.security.JwtKeyGenerator; | ||
import nl.nn.adapterframework.util.SpringUtils; | ||
|
||
@Configuration | ||
public class JwksEndpoint implements ApplicationContextAware { | ||
private final Logger log = LogManager.getLogger(JwksEndpoint.class); | ||
|
||
private @Setter ApplicationContext applicationContext; | ||
|
||
@Bean | ||
public ServletRegistrationBean<JwksServlet> createJkwsEndpoint() { | ||
JwksServlet servlet = SpringUtils.createBean(applicationContext, JwksServlet.class); | ||
log.info("registering servlet [{}]", servlet::getName); | ||
|
||
ServletRegistrationBean<JwksServlet> bean = new ServletRegistrationBean<>(servlet); | ||
bean.setName(servlet.getName()); | ||
bean.addUrlMappings("/iaf/management/jwks"); | ||
|
||
log.info("created IAF API servlet endpoint {}", bean::getUrlMappings); | ||
|
||
return bean; | ||
} | ||
|
||
private static class JwksServlet extends HttpServlet implements ApplicationContextAware { | ||
private JwtKeyGenerator keyGenerator; | ||
|
||
public String getName() { | ||
return "JwksServlet"; | ||
} | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
resp.getWriter().write(keyGenerator.getPublicJwkSet()); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
keyGenerator = applicationContext.getBean("JwtKeyGenerator", JwtKeyGenerator.class); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
console/war/src/main/java/nl/nn/adapterframework/web/SecurityChainConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2023 WeAreFrank! | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package nl.nn.adapterframework.web; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.util.matcher.RequestMatcher; | ||
|
||
import nl.nn.adapterframework.lifecycle.DynamicRegistration; | ||
|
||
@Configuration | ||
@EnableWebSecurity //Enables Spring Security (classpath) | ||
@EnableMethodSecurity(jsr250Enabled = true, prePostEnabled = false) //Enables JSR 250 (JAX-RS) annotations | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class SecurityChainConfigurer { | ||
|
||
private SecurityFilterChain configureAuthenticator(HttpSecurity http) throws Exception { | ||
http.anonymous().authorities(getRolePrefixedAuthorities()); | ||
return http.build(); | ||
} | ||
|
||
// Adds AuthorityAuthorizationManager#ROLE_PREFIX | ||
private List<GrantedAuthority> getRolePrefixedAuthorities() { | ||
return Arrays.asList(DynamicRegistration.ALL_IBIS_USER_ROLES).stream().map(e -> "ROLE_" + e).map(SimpleGrantedAuthority::new).collect(Collectors.toList()); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain configureChain(HttpSecurity http) throws Exception { | ||
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); | ||
http.securityMatcher( new MatchAllButJWKS() ); | ||
http.headers().frameOptions().sameOrigin(); //Allow same origin iframe request | ||
http.csrf().disable(); | ||
http.formLogin().disable(); //Disable the form login filter | ||
http.anonymous().disable(); //Disable the default anonymous filter | ||
|
||
return configureAuthenticator(http); | ||
} | ||
|
||
private static class MatchAllButJWKS implements RequestMatcher { | ||
private static final String JWKS_ENDPOINT = "/iaf/management/jwks"; | ||
|
||
@Override | ||
public boolean matches(HttpServletRequest request) { | ||
return !JWKS_ENDPOINT.equals(request.getServletPath()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "All endpoints except ["+JWKS_ENDPOINT+"]"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.