Apache Shiro in Spring Boot Desktop Standalone Application - java

I once successfully implemented Apache Shiro in a desktop standalone application (without Spring framework or any framework for that matter).
I used INI file to get SecurityManager like so:
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:resources/shiro.ini");
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
I read in one of stackoverflow post that it is not advisable to use shiro INI when using spring.
In my case now I use Spring Boot with Spring Data JPA with Hibernate in this application. What is the best way to configure Apache Shiro so that I can maximise the use of this security framework.
Edit
All examples i have come across, shows examples for web. I need specifically for standalone application. Examples will highly be appreciated.
Solution
After alot of learning and answers from this post, i managed to come up with this solution and created github repo. If anyone wishes to make it better, be my guest.

As I understand doesn't mutter if you are developing a web-based application or a standalone, unless for the webfilters official Apache shiro doc as you can see here this example shows how to properly configure apache shiro for both, web-based and standalone.
You can still use xml-based configurations,as is described here with spring boot and it's easy migrate configuration from INI files to XML using tags like or constructor-args as shown here spring xsd doc
In my opinion use INI file or XML-based configurations has smiilar advanteges and disadvantages, in both methods you can "hot-swap" beans configurations.
For instance if it's easy for you use Java or configurate Shiro on code you can check this github link where you can see an example Class configuration for apache Shiro spring-boot application.
Forget about the web filters and configure the others beans as on this links it should work fine.
As far as i know there is no a good or a worng way talking about use xml or class configurations with spring boot, it just depend on what your needs are.
To finish I code a simple example Spring-boot-standalone with shiro. for you to demostrate an XML-based configuration, this prints the realm bean to show that the context is up and has commented code to fulfill with your needs the rest must be compleate beans with the shiro webpage tutorial.
Greatings.

You can use a yml file to add shiro configurations along with you the application properties.
As an example, take a look at https://github.com/johntostring/spring-boot-shiro

Related

How to integrate Olingo(Odata) in Java SpringBoot Project

Can anyone please help me on how to integrate Olingo (Odata) in a Springboot Java Appln.
I'm pretty new to Spring boot and have implemented one project and wanted it to convert to Oling (Odata).
I have gone through various resources but with a bunch of different approaches not sure how to do it the correct way.
Please let me know if some has worked on it and can guide me.
link to the project on which I applied spring-boot.
If you are trying to integrate OlingoJPA there are a couple of things you will have to do
Implement JPAServiceFactory and initializeODataJPAContext, basically this is about defining the persistence unit and entity manager
Then you can create a Spring Boot Configuration to mount your OData endpoint and initialize the EntityManagerFactory
Then you can point to your database here
An finally you can define the JPA entities you want your service to expose
The full Spring Boot + JPA project sample is located Github. Feel free to go though it, raise an issue or submit a Pull request for impalements

Spring Boot: Determining what autoconfiguration are applied to jersey?

Following the online docs for adding jersey to Sring boot, it appears I just need to include the following package
spring-boot-starter-jersey
Actually, it states that Spring Boot provides automatic configuration by including this package.
Where can I find out what exactly is happening?
If I don't add this package then what is NOT configured?
I tried searching for the package in google but got no specific explanation only saying that it automatically configures, but configures what?
I would like to know a little more of what is happening behind the scenes.
All of the auto-configuration code for all that Spring Boot supports is in the spring-boot-autoconfigure module. If you look through the packages, you will see a jersey package.
The "starter" modules generally do not have any code (of course unless it is a third-party module). How it works is that the code in the auto-configuration has some annotations that are #ConditionalOnXxx, where the condition be anything from a class being on the classpath. If this class is not available, then the auto-configuration will not take place. That's pretty much all adding the jersey starter module does: it adds the jersey dependencies so that the auto-configurer will applied.
Now what exactly is being auto-configured specifically for Jersey? Check out the source for the JerseyAutoConfiguration. Basically what you are going to see is your ResourceConfig being injected into the configurer. From that ResourceConfig, it creates Jersey's ServletContainer (which is the main entry point for Jersey.
Then, depending on our properties configuration, either a FilterRegistrationBean or a ServletRegistrationBean is created as a Spring bean, wrapping Jersey's ServletContainer. Jersey can be created as a Servlet or a Servlet Filter. Whichever one we configure we be used.
And that's it for the Jersey configuration. Spring Boot will get a servlet container (e.g. Tomcat, Jetty) from some other auto-configuration, and take the Filter/ServletRegistrationBean and add the Servlet/Filter to that servlet container.
Also, not really that important, but the auto-configuration also give us some Jackson configuration helpers. For example, instead of configuring our own ContextResolver, we can just configure an ObjectMapper Spring bean.
That's really all you get. It's nothing so spectacular that you couldn't just do it yourself without depending on the auto-configuration.
I would recommend learning a little more about how Spring Boot works to provide the automatic configuration in general.
A good reference is the spring boot reference guide. http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#common-application-properties
You can look at the common application properties in appendix A to see what all spring allows you to configure out of the box.
The Reference guide also gives a high level of what "spring-boot-starter-jersey" if you search for it on the page.
They also have a few samples that you can go through and debug to follow along if that is a way for you to learn.
Hope that gives you a starting point for learning!

Spring Boot: web-app + webservice

Need to create a webapp in java using spring boot which has UI and also it functions as a webservice. (web-app + webservice using spring boot). How to do the configurations ?
I think that this question is a bit wide. You should start by reading some tutorial like :
http://projects.spring.io/spring-boot/
I don't really know a good option to do UI with spring boot, but maybe look at :
https://spring.io/guides/gs/crud-with-vaadin/
You will need to learn and read a bit but I hope it helped you :)
EmCode
Select the SpringBoot example that is most close to your problem space and extend. You can find the samples Here.
Alternatively you can use the Spring Initializr
This is an example of Web app from the original spring page:
https://spring.io/guides/gs/spring-boot/
And this for webservices
https://spring.io/guides/gs/actuator-service/
You only need to start from the first example and add configuration in the 2nd

Spring management extension like JMX

Looking for a management like JMX for spring beans. i wanted to load all the spring beans to the container and using a simple swing gui/jgraphs to show the bean dependencies/details etc
Likewise viewing the spring dependency one can also create a dependency from the ui.
please suggest any open source tool or point me to a code snippet.
Appreciate your help.

First steps in Spring Framework

I have downloaded Spring Framework and using SpringSource dm Server. I just need to write a simple Hello world using Spring Framework. I don't know about the configuration and where to write the JAVA code.
I did a google, but could not find anything that can help me out.
This is a very simple tutorial that outlines a basic setup (although using tomcat).
Edit: Another good example from a different question.
Ummm... "Hello world" programs typically demonstrate languages which Spring is not. What problem are you trying to solve?
It's easy! in 5 steps you can run the hello world using Spring
1. Set up a Maven Project
2. Add Spring Dependency
3. Create the Spring Bean Configuration File
4. Create a Spring Bean
5. Run the Maven Project
Create a Hello class under /src/main/java directory to test the project.
public class Hello {
#SuppressWarnings("resource")
public static void main(String[] args) {
// loading the definitions from the given XML file
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorldService service = (HelloWorldService) context
.getBean("helloWorldService");
String message = service.sayHello();
System.out.println(message);
//set a new name
service.setName("Spring");
message = service.sayHello();
System.out.println(message);
}}
Right click Hello.java and run it as Java Application. The result is:
Hello! Program Creek Readers
Hello! Spring
More details see this tutorial
here others tutorials the Hello World with Spring
Spring 3 hello world example
Spring MVC hello world example
If you wish to learn more,you can see, this tutorials the MKYONG
In this series of tutorials, it’s provides many step by step examples and explanations on using the Spring framework.
Spring Core Core support for dependency injection, transaction management, web applications, data access, messaging, testing and more.
Spring MVC
, a Java Model-View-Contraller (MVC) web framework, which builds on top of the Spring Inversion of control(IoC) framework.
Spring Security, is a flexible and powerful authentication and access control framework to secure Spring-based Java web application.
Spring Data Mongo DB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities.
Spring Batch, is an open source framework for batch processing – execution of a series of jobs. Spring Batch provides classes and APIs to read/write resources, transaction management, job processing statistics, job restart and partitioning techniques to process high-volume of data.
And here, Introduction to the Spring Framework
Get hold of Spring Manning in Action book (google may give it to you or its worth buying if you plan to work on spring more). It will explain to you properly how to start

Categories