我在弹簧安全配置方面遇到了麻烦。我在下面分享我的代码。
我的问题是我想要/user*
url和/admin*
url只有在用户登录我的应用程序时才能访问,我的应用程序有主要的ajax调用,所以我希望没有用户可以访问{ {1}}没有登录的URL。但是当我尝试在Web浏览器中输入URL时,我甚至没有被重定向到登录页面,而是进入了在URL中键入的页面。
所以有人可以帮我解决这个问题。
弹簧security.xml文件
/user*
的web.xml
<?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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
<intercept-url pattern="/user**" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/user/home"
authentication-failure-url="/loginfailed" />
<logout invalidate-session="true" logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select USERNAME, PASSWORD from USER where USERNAME = ?"
authorities-by-username-query="ROLE_USER"
/>
</authentication-provider>
</authentication-manager>
ControllerServlet.java
<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>User</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>User</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/User-servlet.xml,
/WEB-INF/Spring-Datasource.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<!-- Spring Security -->
<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>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>
数据库设计/代码:
@Controller
public class UserController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String getUserLoginPage(){
return "user/index";
}
@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String showErrorLoginPage(ModelMap modelMap){
modelMap.addAttribute("message", "Invalid Login Credentials");
return "user/index";
}
@RequestMapping(value = "/user/home", method = RequestMethod.GET)
public String getUserHomePage(ModelMap modelMap, Principal principal){
//String name = principal.getName();
//modelMap.addAttribute("name", name);
return "user/home";
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String showLogoutPage(ModelMap modelMap){
return "user/index";
}
}
登录表格代码:
create table USER(ID BIGINT NOT NULL AUTO_INCREMENT, USERNAME varchar(20) NOT NULL UNIQUE, PASSWORD varchar(20) NOT NULL, FIRSTNAME varchar(25) NOT NULL, LASTNAME varchar(25) NOT NULL, UPDATED_ON varchar(25) NOT NULL, PRIMARY KEY (ID));
答案 0 :(得分:1)
你的模式错了
pattern="/user/**"
应该是对的。
要更好地了解模式中的星星:
xx/**
表示xx
下面的完整树状结构
担保。xx/*
表示只有xx
中的数据受到保护。xx/*.rar
*
是文件的通配符,因此所有.rar文件
是安全的。希望这有帮助。
答案 1 :(得分:1)
您还应该像对待 authority-by-username-query 一样指定查询 用户按用户名查询强>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select USERNAME, PASSWORD from USER where USERNAME = ?"
authorities-by-username-query="ROLE_USER"
/>
例如
authorities-by-username-query="select u.username, ur.authority from user u, user_roles ur
where u.user_id = ur.user_id and u.username =?"
还要考虑在配置中添加password-encoder,以便在数据库中没有纯文本密码
答案 2 :(得分:1)
有两个问题:
首先,您必须更正地址模式:例如user/**
其次,如果您对安全资源进行了宁静的调用,可能会发生这种情况。
检查萤火虫。您会看到302 redirect
作为对您的请求的回复。在这种情况下,您最好不要使用重定向,而是提供403 access denied
响应并在您的ajax框架内手动处理。