Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;在SecurityContext中未找到身份验证对象;使用@secured方法时_Java_Spring_Spring Mvc_Spring Security_Mybatis - Fatal编程技术网

Java &引用;在SecurityContext中未找到身份验证对象;使用@secured方法时

Java &引用;在SecurityContext中未找到身份验证对象;使用@secured方法时,java,spring,spring-mvc,spring-security,mybatis,Java,Spring,Spring Mvc,Spring Security,Mybatis,我是java开发新手,我正在尝试使用@secured注释在方法级别实现spring安全性 当我调用我的“/登录”时,我得到以下错误: "An Authentication object was not found in the SecurityContext" PS:我没有使用.jsp文件登录,你知道如何管理这个错误吗 这是我的代码部分 @Controller @RequestMapping("/login") public class LoginController { @Autowired

我是java开发新手,我正在尝试使用@secured注释在方法级别实现spring安全性

当我调用我的“/登录”时,我得到以下错误:

"An Authentication object was not found in the SecurityContext"
PS:我没有使用.jsp文件登录,你知道如何管理这个错误吗

这是我的代码部分

@Controller
@RequestMapping("/login")
public class LoginController {
@Autowired
public IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String success(ModelMap map) {       
    userService.addUser("ram", "con1234");
    userService.deleteUser("ABC");
    map.addAttribute("msg", "Done Successfully");
    return "success";
    }  
}

public interface IUserService {
@Secured ({"ROLE_USER", "ROLE_ADMIN"})
public void addUser(String name, String pwd);
@Secured("ROLE_ADMIN")
public void deleteUser(String name);
}


@Repository 
public class UserServiceSec implements IUserService {

@Override
public void addUser(String name, String pwd) {
    System.out.println("user added");
}
@Override
public void deleteUser(String name) {
    System.out.println("user deleted");
}

}
web.xml


马蹄铁
/登录
调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring-config.xml
/WEB-INF/security-config.xml
1.
调度员
/

security-config.xml



您的web.xml中似乎没有DelegatingFilterProxy筛选器。你需要具备以下条件:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

您的web.xml中似乎没有DelegatingFilterProxy筛选器。你需要具备以下条件:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

谢谢shazin的回复。在我将这些行添加到我的web.xml并使用“/login”在服务器上运行项目后,将显示带有用户名和密码的登录页面,然后无论我在用户名和密码中输入了什么,都会出现以下错误:“源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式“,看起来它没有检查spring安全性?我是否遗漏了其他内容?在postman中,当我测试链接时,它会给出登录页的html响应。再次感谢您的帮助。感谢您的回复。在我将这些行添加到我的web.xml并使用“/login”在服务器上运行项目后”显示“使用用户名和密码登录”页面,然后无论我输入了什么用户名和密码,它都会显示以下错误:“源服务器未找到目标资源的当前表示形式,或者不愿意透露存在该表示形式”,看起来它没有检查spring安全性?我是否遗漏了其他内容?在postman中,当我测试链接时,它会给我一个登录页面的html响应。再次感谢您的帮助。
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>