Search code examples
spring-roo

Spring Roo 2.0.RC1 - Spring Security Provider Default and add Spring Security Configuration


Another Problem.

I can not use Spring Security Provider Springlets_Jpa see (Spring Roo 2.0.RC1: use Mysql DB with springlets authentification)

Then is use the default Provider. I have default loginin popup with default user and password show in shell.

I want't to have a user and role table an use login view. I have added Entities User and Role with Roo.

I have no yet spring security configuration.

entity jpa --class ~.model.User --plural Users --table USER --sequenceName USER_ID_SEQ --identifierStrategy AUTO --identifierColumn USER_ID --versionField version --versionType java.lang.Long --versionColumn VERSION --entityFormatExpression "User: #{firstname} #{lastname} (#{username})"
entity jpa --class ~.model.Role --plural Roles --table ROLE --sequenceName ROLE_ID_SEQ --identifierStrategy AUTO --identifierColumn ROLE_ID --versionField version --versionType java.lang.Long --versionColumn VERSION --entityFormatExpression "#{rolename}"

focus --class ~.model.User
field string --fieldName username --column USERNAME --notNull --unique --comment "Username des Nutzers" --regexp ^[a-zA-Z0-9_]{4,}$ 
field string --fieldName password --column PASSWORD --notNull --comment "Passwort des Nutzers" --regexp ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$
field string --fieldName firstname --column FIRST_NAME --notNull --comment "Vorname des Nutzers"
field string --fieldName lastname --column LAST_NAME --notNull  --comment "Nachname des Nutzers"
field string --fieldName email --column EMAIL --notNull --unique --comment "Email des Nutzers" --regexp ^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
focus --class ~.model.Role
field enum --fieldName rolename --column ROLENAME --type ~.model.restricted.RoleType --notNull --comment "Rollentyp"
field set --fieldName userlst --type ~.model.User --mappedBy role --joinColumnName ROLE_ID --permitReservedWords --comment "Liste der User mit diesem Typ"

When i start webapp i acbb create Roles and User. I can select Roles in create User view.

Now i add spring security configuration class (very simple allow all without login)

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll();
    }

}

When i now start the webapp and create User i can't select Role from List. I get WARN

2017-03-30 09:12:55.088  WARN 6644 --- [nio-8082-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWri
tableException: Could not write content: No converter found capable of converting from type [de.quin
tra.rechnungspruefung.model.Role] to type [java.lang.String] (through reference chain: io.springlets
.data.web.select2.Select2DataWithConversion["results"]); nested exception is com.fasterxml.jackson.d
atabind.JsonMappingException: No converter found capable of converting from type [de.quintra.rechnun
gspruefung.model.Role] to type [java.lang.String] (through reference chain: io.springlets.data.web.s
elect2.Select2DataWithConversion["results"])

And now role show in List. What is wrong?


Solution

  • Some weeks ago, I detected a problem in Spring Security and I created the following issue in their repository:

    https://github.com/spring-projects/spring-security/issues/4202

    Seems like if a @Configuration class extends the WebSecurityConfigurerAdapter abstract class (like in the code generated by Spring Roo), some component is trying to @Autowired a ConversionService instance before the formatters have been registered in the Spring context, so the addFormatters method doesn't include any formatters on it.

    A simple work-around that will solve your problem and you'll be able to use Spring Security in your project is to @Override the setContentNegotiationStrategy method in the generated SecurityConfiguration class without include the @Autowired annotation.

    The following example looks how to override this method correctly. (In this sample the code is commented)

    https://github.com/jcagarcia/proofs/blob/master/spring-security-and-formatters/src/main/java/org/springframework/roo/petclinic/config/security/SecurityConfiguration.java#L54

    If this solves your problem, will be great that you comment on the issue saying that you have the same problem.

    Hope it helps,