小编clo*_*her的帖子

如何在Spring MVC中模拟安全上下文进行测试

我需要测试一些受保护的URL,因此我需要在我的测试中设置一个模拟安全上下文(junit).
特别是我需要使用经过身份验证的用户对我的Web应用程序执行一些获取和发布.下面是我的代码,我能够创建这样的安全上下文,但我需要将它注入'MockMvc'对象.
我在安全上下文中设置了身份验证对象并且它正常工作,'SecurityContextHolder.getContext().getAuthentication().getPrincipal()'的输出结果是chanelle.evans@616747.com但是当我在/ profile上调用GET时有一个断言错误,因为我被重定向到我的登录页面,而不是/ profile.

@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/security.xml", "classpath:spring/view.xml"})
@ActiveProfiles("default")
@RunWith(SpringJUnit4ClassRunner.class)
public class AuthenticationTest {

@Autowired
WebApplicationContext ctx;

private MockMvc mockMvc;

@Autowired
private FilterChainProxy springSecurityFilterChain;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilters(springSecurityFilterChain).build();

    //@formatter:off

    UserDetailsLogic userDetailsLogic = null;
    userDetailsLogic = ctx.getBean(UserDetailsLogic.class);
    final UserDetailsImp userDetailsImp = new UserDetailsImp();
    userDetailsImp.setAccountId(1001);
    userDetailsImp.setUserId(8001);
    userDetailsImp.setPassword("a378c92df7531df6fdf351f7ae1713f91f2dd2d45b9c6e1a8b02736ee3afec6595ff60465e9cb8da");
    userDetailsImp.setUsername("chanelle.evans@616747.com");
    userDetailsImp.setEmail("chanelle.evans@616747.com");

    final Collection<GrantedAuthorityImplementation> authorities= new ArrayList<GrantedAuthorityImplementation>();
    authorities.add(new GrantedAuthorityImplementation("ROLE_USER")); …
Run Code Online (Sandbox Code Playgroud)

junit spring spring-mvc spring-security spring-test-mvc

17
推荐指数
1
解决办法
2万
查看次数

以下验证是否表示该字段不能为空?(@Size注释)

我在Spring MVC Form bean中使用以下属性javax.validation.constraints来验证表单bean,如下所示:

public class MyForm {
    @Size(min = 2, max = 50)
    private String postcode;

    // getter and setter for postcode.
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:@Size(min = 2)属性是不是null因为它总是需要长度大于2的意思.我之所以说这是因为@NotNull在同一个包中存在约束,因此@NotNull如果我应该这样做会使约束变得多余在上面的bean中使用它.

java validation spring-mvc

13
推荐指数
1
解决办法
9233
查看次数

JsonPath JUnit用于点的转义字符

我有一个名为template.welcome.email的json字段,我正在编写一个单元测试,检查服务器的回复中是否存在该字段,但我找不到该字段名称中的点的转义符.我的测试代码是:

@Test
public void testEmailTemplates() throws Exception {     
    mockMvc.perform(get("/emailTemplates")
        .contentType(MediaType.APPLICATION_JSON)
        .locale(Locale.UK)
        .accept(MediaType.APPLICATION_JSON))

        .andDo(print())
        .andExpect(status().isOk())

        .andExpect(jsonPath("$.template.welcome.email").exists())

        .andExpect(redirectedUrl(null))
        .andExpect(forwardedUrl(null));
}
Run Code Online (Sandbox Code Playgroud)

但我得到以下异常,因为这些点被解释为路径:

java.lang.AssertionError: No value for JSON path: $.template.welcome.email, exception: invalid path
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:74)
at org.springframework.test.util.JsonPathExpectationsHelper.exists(JsonPathExpectationsHelper.java:121)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers$3.match(JsonPathResultMatchers.java:77)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
at 
Run Code Online (Sandbox Code Playgroud)

你知道jsonPath的任何转义字符吗?

java junit spring json spring-mvc

12
推荐指数
3
解决办法
1万
查看次数

默认值ON UPDATE Liquibase

我正在使用Liquibase生成MySQL和HSQLDB数据库.
在几个表中,我有一个名为'last_modified'的列,它是该特定记录上次更新的TIMESTAMP.

<changeSet author="bob" id="7">
    <createTable tableName="myTable">
        <column autoIncrement="true" name="id" type="INT">
            <constraints nullable="false" primaryKey="true" />
        </column>
        <column name="name" type="VARCHAR(128)">
            <constraints nullable="false" />
        </column>
        <column name="description" type="VARCHAR(512)" />
        <column defaultValueBoolean="true" name="enabled" type="BIT">
            <constraints nullable="false" />
        </column>
        <column name="last_modified" type="TIMESTAMP"/>
    </createTable>
    <modifySql dbms="mysql">
        <append value=" engine innodb" />
    </modifySql>
</changeSet>
Run Code Online (Sandbox Code Playgroud)

我注意到如果我使用MySQL,那么为该列生成的SQL是:

`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Run Code Online (Sandbox Code Playgroud)

虽然如果我使用HSQLDB,在更新的情况下没有任何反应,但我希望MySQL数据库具有相同的行为,更新时的默认值等于CURRENT_TIMESTAMP.

如何将CURRENT_TIMESTAMP设置为ON UPDATE的默认值?

mysql sql database hsqldb liquibase

10
推荐指数
3
解决办法
6492
查看次数

否定CloudFormation模板中的条件

我有以下条件,因此我创建了一些资源,而如果不满足那个条件,那么我创建其他资源.

Conditions:
  ISProduction:
    "Fn::Equals":
      - !Ref Environment
      - staging
  ISNotProduction:
      "Fn::Not":
        - !Ref ISProduction
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用上面的代码段评估模板时,我收到错误:

模板错误:每个Fn :: Not对象都需要一个布尔参数

如何否定云形成模板中的条件?或者我如何使用ISProduction的否定?

我也在资源创建中尝试了下面的条件,但是我但模板没有通过验证,因为"每个条件成员必须是一个字符串".

Condition:
      "Fn::Not":
        - !Ref ISProduction
Run Code Online (Sandbox Code Playgroud)

cloud amazon-web-services aws-cloudformation

10
推荐指数
1
解决办法
7489
查看次数

Selenium WebElement.click()与Javascript点击事件

我想知道调用WebElementclick()方法与通过id查找元素和使用JavaScript 触发事件之间的区别是什么.click

为了清楚第一种方法,我称之为WebElement.click()的一个实例:

myWebElement.click();
Run Code Online (Sandbox Code Playgroud)

第二种技术是:

((JavascriptExecutor)driver).executeScript("document.getElementById('myElementID').click()");
Run Code Online (Sandbox Code Playgroud)

我很想知道这两种点击web元素的技术之间的差异,以及每种技术的优点和缺点.

javascript java selenium selenium-webdriver

9
推荐指数
1
解决办法
2万
查看次数

如何设置Slack提醒仅通知频道中的人员子集

我需要在频道中设置提醒,但此提醒不应向频道中的所有人发送通知,而只会向特定子集发送通知.
我尝试了下面显示的内容.

/reminder @user1 @user2 @user3 A message every 30 days 
Run Code Online (Sandbox Code Playgroud)

但是当我验证它时,松弛添加"在第一个用户之后,这告诉我提醒将仅发送给第一个用户而不是其他用户.

I will remind @user1 “@user2 @user3 A message” at 9AM Monday, April 1st.
Run Code Online (Sandbox Code Playgroud)

松弛是否只将通知发送给user1或所有人?(如上所述).
Slack是否具有向多个用户发送提醒而不打扰整个频道的功能?
我们不想创建单独的频道.

chat slack-api slack

9
推荐指数
1
解决办法
4118
查看次数

即使渴望获取,Hibernate也不加载一对多关系集

我有以下休眠映射。

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "product")
private Set<ProductLicense> productLicenses = new HashSet<ProductLicense>(0);


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "product_id", nullable = false)
private Product product;
Run Code Online (Sandbox Code Playgroud)

但是,当我调用product.getProductLicences()时,即使在事务方法内部,也总是得到一个空的Set。

sessionFactory.getCurrentSession().get(Product.class, productId))
            .getProductLicenses()
Run Code Online (Sandbox Code Playgroud)

以下是ProductDaoImpl类

@Repository
public class ProductDaoImpl implments ProductDao{

@Autowired
private SessionFactory sessionFactory;

@Override
public Product getProduct(Integer productId)
{

    Product result = (Product) sessionFactory.getCurrentSession().get(Product.class, productId);
    Hibernate.initialize(result.getProductLicenses());
            //sessionFactory.getCurrentSession().refresh(result);
    return result;

}
   .. other methods

}
Run Code Online (Sandbox Code Playgroud)

另外,如果我调用Hibernate.initialize(product); 在表之间不执行任何连接。指令sessionFactory.getCurrentSession()。get(Product.class,productId); 不会对数据库产生任何查询(我的属性show_sql等于true)。但是如果我取消注释sessionFactory.getCurrentSession()。refresh(result); 我可以看到sql,并且该集合已加载,但我不明白为什么在其他情况下未加载。但是我无法理解映射中的错误。

产品类别为:

@Entity
@Table(name = "product")
public class Product {

@Id
@GeneratedValue(strategy = …
Run Code Online (Sandbox Code Playgroud)

java orm spring hibernate hibernate-mapping

7
推荐指数
1
解决办法
5964
查看次数

如何参数化字符串并替换参数

我有一个由最终用户输入的字符串,这个字符串应该是格式Showing {0} to {1} of {2},我想用大括号中的参数替换我从后端计算的数字.就像属性文件中的字符串一样.
我怎样才能做到这一点?

样本输入:

Showing {0} to {1} of {2} 
Run Code Online (Sandbox Code Playgroud)

样本输出:

Showing 1 to 12 of 30
Run Code Online (Sandbox Code Playgroud)

java string java-ee

5
推荐指数
2
解决办法
7600
查看次数

<service-name> dead但pid文件存在

我的服务器上的服务崩溃了

service <service-name> status
Run Code Online (Sandbox Code Playgroud)

我死了,但pid文件存在,然后如果我试图停止我得到的服务

Shutdown <service-name>: bash: line 0: kill: (5018) - No such process
ERROR: could not stop <service-name>:  <service-name> dead but pid file exists
Run Code Online (Sandbox Code Playgroud)

然后我删除/var/run//.pid和var/lock/subsys /中的文件,但它没有帮助.我该怎么办才能真正重启服务?

linux service tomcat cassandra

5
推荐指数
1
解决办法
5万
查看次数