这是我的代码。我不知道这有什么问题。
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Security Application</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</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/spring-security.xml,
/WEB-INF/spring-database.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>
</web-app>
<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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
<!-- Select users and user_roles from database -->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"select USERNAME as username, PASSWORD as password,'true' as enabled from USERS where USERNAME=?"
authorities-by-username-query=
"select USERNAME as username, ROLE as role from USER_ROLES where USERNAME =? " />
</authentication-provider>
</authentication-manager>
</beans:beans>
<beans xmlns="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">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="sekhar" />
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MainController {
@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView defaultPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This is default page!");
model.setViewName("hello");
return model;
}
@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Login Form - Database Authentication");
model.addObject("message", "This page is for ROLE_ADMIN only!");
model.setViewName("admin");
return model;
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
//for 403 access denied page
@RequestMapping(value = "/403", method = RequestMethod.GET)
public ModelAndView accesssDenied() {
ModelAndView model = new ModelAndView();
//check if user is login
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
System.out.println(userDetail);
model.addObject("username", userDetail.getUsername());
}
model.setViewName("403");
return model;
}
}
Apr 21, 2017 9:05:31 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING:
create table users(username varchar(90) primary key, password varchar(90), enabled smallint default 1);
create table user_roles(user_role_id int,username varchar(90) not null , role varchar(90) not null, constraint fk_username foreign key(username) references users(username),constraint user_role_pk primary key(user_role_id));
create sequence user_role_pk start with 1;
insert into users values('abc','123456',1);
insert into users values(2,'alex','123456');
insert into user_roles(username, role,user_role_id) values('abc','ROLE_ADMIN',1);
insert into user_roles values(2,'alex','ROLE_USER');
您真的这样配置了SpringSecurityFilterChain
吗?
<!-- 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>
如果是,请尝试通过以下方式配置
:
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是我的Spring安全豆 这是我的MVC-dispatcher servlet 这是我的web.xml
问题内容: 我已经检查了关于stackoverflow的几乎所有相关文章,但是我无法解决我的问题。 Here is the code: web.xml: spring-servlet.xml: myController: Web Pages/index.jsp: Web Pages/WEB-INF/jsp/hello.jsp: 因此,当我启动该应用程序时,index.jsp已正确加载,但是当我单击
早上,我已经检查了这个问题的大部分答案(在名为DispatcherServlet的DispatcherServlet中找不到URI为的HTTP请求的映射),以及(在名为“DispatcherServlet”的DispatcherServlet中找不到URI为[/ChickenTest/index]的HTTP请求的映射),但我仍然得到“在名为“bmoa”的DispatcherServlet中找不到U
我已经查阅了几乎所有关于stackoverflow的相关文章,但我就是不能解决我的问题。 下面是代码:web.xml: spring servlet。xml: myController: 网页/index.jsp: 网页/WEB-INF/jsp/hello.jsp: 所以当我启动应用程序时,索引。jsp加载正确,但当我点击href导航到hello时。jsp我收到一个404错误,服务器日志显示: 我
我的web.xml applicationContext as 调度器servlet作为 与包com中相同的控制器。思维边缘。特斯塔普 当我点击. 警告:在带名称的DispatcherServlet中找不到与URI[/TestApp/samp]的HTTP请求的映射 此外,当Spring开始时,我没有看到Testapp/示例加载的处理程序 我猜我的组件扫描不起作用。为什么?还有其他原因吗 编辑:调度
当我在JBoss上启动项目时,在名为“appServlet”的DispatcherServlet中,找不到URI为[/myappname/]的HTTP请求的映射。在将访问方法更改为会话后,在解决此处描述的另一个问题:“未找到当前线程的会话”后出现了此问题 在一切顺利之前。我使用的是ApacheTiles 2。我读了一些类似的问题,但我找不到有效的解决方案。 这是我的没有HiberNate配置的文件