Monday, July 31, 2017

spring security fails checking password



>PROBLEM

The security layer fails to authenticate an user without any apparent reason.


>SOLUTION
There are many possible reasons.
Among other possibilities, check:

1. Fields' length checking.
Check the fields' lengths against the model class.
They must match.

2. Check the password's field configuration.
Usually, encryptors generates encrypted strings with length=64, so the database shall provide compatible value.


>ENV
spring boot 1.5.4.RELEASE
java 8
maven
eclipse

Sunday, July 30, 2017

spring boot: required a bean of type ... that could not be found , Consider defining a bean of type


PROCEDURE TO GUIDE YOU THROUGH HARD ISSUES ... MYSTERIOUS ONES
IN THE CONTEXT DESCRIBED HERE, AND POSSIBLY EXTENSIBLE TO OTHERS


>PROBLEM

Attempt of injecting a bean using Spring Boot and Maven fails with the following message:

Parameter 0 of method setStrongEncryptor in br.com.adr.thylab2.service.security.EncryptionSvcImpl required a bean of type 'org.jasypt.util.password.StrongPasswordEncryptor' that could not be found.
Action:
Consider defining a bean of type 'org.jasypt.util.password.StrongPasswordEncryptor' in your configuration.


>SOLUTION

1. First check your scan configuration.

Go to the class that it is used to start the app.
At @SpringBootApplication annotation check value offered to "scanBasePackages" if it contains a parent path for the class where the problem happens. Sometimes, a class becomes out of the subdirectory tree referenced by the scan value. If there isn't a parent path to the complaining class, add it.
@SpringBootApplication(scanBasePackages={"br.com.adr.thylab2", "my another package here"})

Example:

@SpringBootApplication(scanBasePackages={"br.com.adr.thylab2"})
public class Thylab2App extends SpringBootServletInitializer {
public static final String VERSION = "2.0.0-RELEASE";

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Thylab2App.class);
}
        public static void main(String[] args) {
             SpringApplication.run(Thylab2App.class, args);
        }    
}

2. Check your @Service or @Component annotations in the related classes, those with relationships concerned to the issue.
Is it missing an annotation?
Is it annotated as it should be?


3. Is the missing bean defined?
An alternative is to supply the missing bean using a common class.
For instance:

@Configuration
public class CommonBeans {

    @Bean
    public StrongPasswordEncryptor strongEncryptor(){
        StrongPasswordEncryptor encryptor = new StrongPasswordEncryptor();
        return encryptor;
    }
}


4. Check the library versions.
For instance, if using Maven, go to pom.xml and try to update the version of the related packages, or yet, try to downgrade.
Sometimes, one library causes issues with certain versions of another ones.


5. Eventually, rollback and proceed a step-by-step checking.
If the problem still persists, the issue may be caused by more than one issue, which exception thrown on the stack may interfere with the final results to help finding the solution.
This approach is radical but very effective.

First, you make the famous "backup" of the current project. Just do it, please!
A simple copy of it is enough.
If using versioning, for instance Git or SVN, you may get a previous point prior to the introduction of the new stuff related to the issue. This is the whole point, no matter which version control system you are working with!

5a. Check if your classpath contains the necessary references.
A simple way of doing this is including some testing code that references it.
For instance, if using Spring Boot, you could simply do, like below, in a straight manner in order to skip another possible setbacks to check if your classpath is properly configured.

@SpringBootApplication(scanBasePackages={"br.com.adr.thylab2"})
public class Thylab2App extends SpringBootServletInitializer {
public static final String VERSION = "2.0.0-RELEASE";

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Thylab2App.class);
}

  public static void main(String[] args) {

StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
String encryptedPassword = passwordEncryptor.encryptPassword("");

             SpringApplication.run(Thylab2App.class,  args);
        }    
}

5b. Bring to the code one interface/class set at a time.
Begin with the simplest classes, bringing just one interface/class set, and testing.
Test one after the other, never more than one by its turn.
You'll probably note that this procedure will show more than one issue, but working one by one, helps the compiler to guide you to the solution.

I do sincerely hope that this "laborious and boring" procedure may help you, but surely helped me when I was desperately lost, more than once, trying to find out a mysterious issue.  :-)


>ENV
spring boot 1.5.4.RELEASE
java 8
maven
eclipse

Wednesday, July 19, 2017

MySQL 5.5 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails


>PROBLEM

Running the script like this:
  \. myScript.sql
fails and returns the message:
  ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

Nevertheless, if running the command straight on the console, it may work.



>SOLUTION

This issue was solved checking the file's encoding versus the database encoding and matching them.
So, the database was UTF-8, and the script was using the same encoding.



>ENV
Windows
MySQL 5.5

Sunday, July 9, 2017

spring: thymeleaf: There was an unexpected error (type=Not Found, status=404)



>PROBLEM

Pointing to URL returns 404.


Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Jul 09 11:37:35 BRT 2017



There was an unexpected error (type=Not Found, status=404).
No message available





>SOLUTION

The web site server will typically generate a "404 Not Found" message when the resource is not found, like a broken or dead link.

So, go to the Controller class and check the right link to be used.




Attention to the detail that sometimes, the application is not configured to use an extra identification in the URL. This is the case in this example.

Usually, the app's access is done using its name, like this:
  
   http://localhost:8080/appName/index

If the configuration does not use it, then we go straight to the controller's reference, like this:

  http://localhost:8080/index




















Friday, July 7, 2017

spring: thymeleaf: Neither BindingResult nor plain target object for bean name



>PROBLEM

The request returns the following error message:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jul 07 17:02:46 BRT 2017
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (login)




>SOLUTION

In this example, it missing the object requested by the html template at:



Check your parameters on the respective method in the controller's class.



Fixing the bug, including the missing parameter:




Problem solved.


>ENV
Java
Spring boot
Thymeleaf
Eclipse

Thursday, July 6, 2017

spring: thymeleaf: SpringInputGeneralFieldAttrProcessor Error



>PROBLEM

Running spring and thymeleaf, returns the following error page:


Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jul 06 17:16:04 BRT 2017
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (login)




>SOLUTION

Since the log is pointing to login, open the class which should have the respective attribute.
To find out, go to the page which is returning the error. The page may be found in the controller.
In this example we get:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String addNewLogin(Login login) {
return "login";
}

Opening the page, check the object pointed by thymeleaf's "th:object" property in form tag.
In this example is "th:object="${login}".
The error message means that the parser is complaining that it was not found the attribute login for Login class.








Opening the respective Login class, you get instead of "login" the "signin" attribute.


]


Switch the wrong attribute by the correct one, as below:









 Now refresh page, and that's it!  :-)


>ENV
Java
Spring boot
Thymeleaf
Eclipse

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...