Spring Roo 2.0.RC1 - Spring Security Provider默认并添加Spring Security Configuration

时间:2017-03-30 07:15:51

标签: spring-roo

另一个问题。

我无法使用Spring Security Provider Springlets_Jpa看到(Spring Roo 2.0.RC1: use Mysql DB with springlets authentification

然后使用默认的Provider。 我有默认的loginin popup,默认用户名和密码显示在shell中。

我希望让用户和角色表成为使用登录视图。 我已经在Roo中添加了实体用户和角色。

我还没有弹簧安全配置。

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"

当我启动webapp时,我会创建角色和用户。我可以在创建用户视图中选择角色。

现在我添加spring安全配置类(非常简单,允许所有没有登录)

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();
    }

}

当我现在启动webapp并创建用户时,我无法从列表中选择角色。 我得到了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"])

现在角色显示在List中。 怎么了?

1 个答案:

答案 0 :(得分:0)

几周前,我在Spring Security中发现了一个问题,我在他们的存储库中创建了以下问题:

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

似乎@Configuration类扩展了WebSecurityConfigurerAdapter抽象类(如Spring Roo生成的代码中),某些组件正在尝试@Autowired ConversionService个实例在formatters已在Spring上下文中注册之前,addFormatters方法不包含任何格式化程序。

一个简单的解决方案可以解决您的问题并且您将能够在项目中使用Spring Security,而不是@Override生成的setContentNegotiationStrategy类中的SecurityConfiguration方法包括@Autowired注释。

以下示例介绍如何正确覆盖此方法。 (在此示例中,代码已注释)

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

如果这可以解决您的问题,那么comment on the issue说你有同样的问题会很棒。

希望它有所帮助,