Spring Security - 测试

时间:2017-01-11 19:54:22

标签: java spring spring-security

当我想运行测试时,我遇到了这个问题:http://pastebin.com/5fDj4yye

我有这个security.xml文件(我没有通过类配置安全性,所以我不能使用filterchainproxy):

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

<beans:import resource="servlet-context.xml" />

<beans:bean name="bcrypt"
           class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" >
    <beans:constructor-arg name="strength" value="11" />
</beans:bean>

<global-method-security secured-annotations="enabled" />

<http auto-config="true" >
    <intercept-url pattern="/notes/**" access="authenticated" />
    <intercept-url pattern="/auth/**" access="permitAll" />
    <intercept-url pattern="/accessDenied" access="permitAll" />
    <intercept-url pattern="/" access="permitAll" />

    <access-denied-handler error-page="/accessDenied" />

    <logout logout-success-url="/auth/login?logout"  />
    <form-login
            default-target-url="/notes/"
            login-page="/auth/login"
            login-processing-url="/j_spring_security_check"
            username-parameter="username"
            password-parameter="password"
    />
    <remember-me data-source-ref="dataSource" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsServiceImpl">
        <password-encoder ref="bcrypt" />
    </authentication-provider>
</authentication-manager>

<beans:bean id="userDetailsServiceImpl" class="ru.mrchebik.service.impl.UserDetailsServiceImpl" />

这是一个测试:

package ru.mrchebik.service;

/**
* Created by mrchebik on 05.12.16.
*/
@DirtiesContext
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:**/servlet-context.xml")
@WebAppConfiguration("webapp/WEB-INF/spring/appServlet")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ServiceTest {
private static User user = new User("test", "test");
private static List<Note> findNotes;
private static User finduUser;

@Resource
private NoteService noteService;

@Resource
private UserService userService;

@Test
public void test01Add() throws Exception {
    userService.add(user);
    noteService.add(new Note(user, "Title", "Text"));
}

@Test
public void test02Find() throws Exception {
    findNotes = noteService.findNotes(userService.findUser("test").getUserId());
    finduUser = userService.findUser("test");
}

@Test
@WithMockUser(roles = {"ADMIN"})
public void test03Clear() throws Exception {
    noteService.remove(findNotes.get(0).getId());
    userService.remove(finduUser.getUserId());
}

我不知道我在security.xml文件中的错误。

0 个答案:

没有答案