security.xml to java config how to reference position="LAST" - spring-security

So, I'm trying to migrate to Spring Security 4.0.4 and use java config in stead of security.xml
How do I implement
<custom-filter ref="userAgentFilter" before="LAST" />
in java config
http.addFilterBefore(new UserAgentFilter(),LAST??)

There is no concept of "LAST" in Spring Security Java Configuration. Everything is ordered based on the Filter class. Instead, you can insert it after your last Filter. For example
http
.addFilterAfter(filter, SwitchUserFilter.class)

Related

Execute servlet filters before the Spring Security filter chain

I'm introducing Spring Web Security in an already existing website. Now there has occured a problem:
I want my custom filters to be executed in the Tomcat filter chain before the Spring Security filter chain. I don't want them to be a part of this Spring Security filter chain.
All I have found is a solution in Spring Boot (see "Spring - How to insert a filter before SpringSecurityFilterChain") - but I'm not using Spring Boot.
Update 22.11.2016:
Here is a part of my web.xml - only the custom filter is added here:
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
The Spring Security filter chain is added by defining an AbstractSecurityWebApplicationInitializer:
public class MySecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
...
}
The outcome: The Spring security filters go first and only after this my custom filter.
How to swap this?
If you define Web Security filter in your web.xml before springSecurity Filter declaraion,then this filter will not be part of spring security filter chain and your filter will execute before spring security filter chain.
Or if you want filter to exeute at web server level,You can define filter in tomcat web.xml which is avaiable in conf/ directory.This filter will execute before your application filter chain.
But this filter will execute for all web applications runtime under your tomcat.
note that you'd also need to make the filter class available to all web applications, probably by putting it in the lib/ directory of Tomcat).

Neo4j configure performance properties in embedded Spring-Data application

How can I configure properties like neostore.nodestore.db.mapped_memory or nodeostore.relationshipstore.db.mapped_memory as described at http://docs.neo4j.org/chunked/stable/configuration-io-examples.html?
I am using the Spring Cineast-Demo-Project.
I guess, I have to configure it in applicationContext.xml, where is also the neo4j store defined:
<neo4j:config storeDirectory="target/data/graph_1000_nodesEdges.db" />
Any hint, where I can configure these performance-parameter?
Look at this link, example 2.1.8, it shows how to pass configuration parameters to the neo4j engine.

Configure spring security ldap-server attribute to use different url based on deployed environment

We are using spring security and have it working well. I am trying to figure out one thing that has not being obvious - how do I configure ldap-server attribute to use different url based on deployed environment?
This is what I have that is working:
<ldap-server url="ldap://testserver:port/o=blah" manager-dn="cn=bind,ou=Users,o=blah" manager-password="password"/>
<authentication-manager id="authenticationManager" alias="authenticationManager">
<ldap-authentication-provider
user-search-filter="(cn={0})"
user-search-base="ou=Users"
group-search-filter="(uniqueMember={0})"
group-search-base="ou=groups"
group-role-attribute="cn"
role-prefix="none">
</ldap-authentication-provider>
Now, how do I configure it to use a different url based on deployed environment?
thanks in advance,
Sharath
I've done that with Spring profiles:
In your spring.*.xml config file use this at the end of your file:
<beans profile="production">
...
</beans>
<beans profile="local">
...
</beans>
As VM Arguments the used profile must be provided:
-Dspring.profiles.active=production
Regards
You can use the url as variables and set them in a properties file.
To change the properties file should be easier. I know you can do that with Maven - with jar or war plugin depending on packaging, including generating two (or more) packages with one execution - but I suppose you can with Ant or other managers too.
Of course, you could use that solution to change the whole xml, but it's easier to do that with a properties file because that way, when changing the configuration, the markup will not be in the way, only variables and values.

Apache Camel URL Escaping with space

I have a simple Camel Route which processes the incoming Http request and then routes to other Http consumers based on the Resource path. Everything works fine but I hit the java.net.URISyntaxException: Illegal character in path when used a space in the path.
Other special characters seem to work fine.
I am building a RESTful APIs and currently using the browser to test my APIs.
Here is the Spring DSL of my route:
<route id="API">
<from uri="jetty:http://0.0.0.0/api?matchOnUriPrefix=true"/>
<bean ref="basicAuthBean"/>
<choice>
<when>
<simple>${in.header.CamelHttpPath} contains 'blah1'</simple>
<to uri="http://localhost:10001/api?bridgeEndpoint=true&throwExceptionOnFailure=false"/>
</when>
<when>
<simple>${in.header.CamelHttpPath} contains 'blah2'</simple>
<to uri="http://localhost:10002/api?bridgeEndpoint=true&throwExceptionOnFailure=false"/>
</when>
</choice>
</route>
I enabled trace on the Camel Context and found that the CamelHttpPath had already replaced the escape character "%20" with a space. I also saw that there was CamelHttpUri which had not escaped the special character.
As a hack in my Spring DSL I added the following before the choice element:
<setHeader headerName="CamelHttpPath">
<simple>${in.header.CamelHttpUri}</simple>
</setHeader>
This solved my problem but I am pretty sure there is a better way to do this. Did I miss setting some properties or is my route DSL not accurate?
Also, what is the difference between the CamelHttpPath and CamelHttpUri?
I should also mention that I am using the Camel Context in Apache ServiceMix 4.4.2 and the Camel Version used is 2.8.5.
Thanks in advance for the help!
I think we have fixed this in later Camel releases due to be released like Camel 2.10.4 etc. I remember working on that space fix.
https://issues.apache.org/jira/browse/CAMEL-5504
As Camel 2.8.x is no longer supported you would need to patch the issue yourself.
Or upgrade to ServiceMix 4.5.0 which includes Camel 2.10.3.

Blueprint dependency injection within a bundle

I've come across a case where I want to use Blueprint (Aries) to resolve a dependency at run-time and the implementation is defined in the same bundle which requires it and will not be used in any other bundles. I am abstracting the implementation within this bundle to make it easier to mock the dependency when unit testing. If I put this service in its own bundle, it would lead to poor cohesion.
At run-time, the Blueprint says it is waiting for dependencies. How can I use Blueprint to realize dependency injection within a bundle?
<!-- Interface -->
<reference id="modelEntityMapper" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />
<!-- Implementation defined within same bundle -->
<bean id="modelEntityMapperImpl" class="org.example.blog.rest.cxf.server.model.impl.ModelEntityMapperImpl" />
<service ref="modelEntityMapperImpl" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />
<!-- Object which has dependency -->
<bean id="posts" class="org.example.blog.rest.cxf.server.BlogResourceImpl">
<property name="modelEntityMapper" ref="modelEntityMapper" />
</bean>
Edit
I just tried the suggestion from #christian-scheider and Blueprint is still waiting for some service to satisfy ModelEntityMapper
The XML
<!-- Interface -->
<reference id="modelEntityMapper" interface="org.example.blog.rest.cxf.server.model.ModelEntityMapper" />
<!-- Implementation defined within same bundle -->
<bean id="modelEntityMapperImpl" class="org.example.blog.rest.cxf.server.model.impl.ModelEntityMapperImpl" />
<!-- Object which has dependency -->
<bean id="posts" class="org.example.blog.rest.cxf.server.BlogResourceImpl">
<property name="modelEntityMapper" ref="modelEntityMapperImpl" />
</bean>
The Log
Bundle rest-cxf-server is waiting for dependencies [(objectClass=org.example.blog.rest.cxf.server.model.ModelEntityMapper)]
Can you just refer to the bean of the service directly? If you define the service and the service reference in the same blueprint file then using an OSGi service does not make that much sense.
I was unable to find detailed documentation on the Aries site related to referencing in bundles, so I'm going to reference the Eclipse Gemini Blueprint implementation documentation (formerly Spring Dynamic Modules). See the warning in section 9.2.1.1 of their documentation. Yes, technically this is related to their implementation, but I believe it's likely a similar story in Aries.
It is an error to declare a mandatory reference to a service that is also exported by the same bundle, this behaviour can cause application context creation to fail through either deadlock or timeout.
In a nutshell you typically either import (reference) an OSGi service or you export an OSGi service in the same bundle, usually you don't try to do both in a single bundle.
If you want this bundle to export a service of type ModelEntityMapper, then you'll need to export it with the service element. When other beans need a reference within the same bundle, you would use the ref attribute like you're using it. In this case, you would not need the reference element at all, but instead use the service element.
If you're not going to use the ModelEntityMapper bean outside of this bundle, you don't need to use a reference or service element in the configuration at all. You should be able to use it in the ref attribute without exporting it as an OSGi service - it's basically a bean internal to that bundle. In this case, you should be able to remove the reference element altogether: the <bean id="modelEntityMapperImpl" ... will create a bean internal to the bundle, and the <property name="modelEntityMapper" ref="modelEntityMapperImpl" /> element should be able to use that bean internally to the bundle.
If you want to import a reference of type ModelEntityMapper from OSGi if available, else use an internally defined fallback, that gets more complicated. You'd have to declare a non-mandatory reference and inject that reference into your class along with the internally defined bean and then have defaulting logic that checks the availability of them. Alternatively you could just define the implementation in a separate bundle from the interface.

Resources