摘要:创建一个工程在里面添加依赖,依赖不要随便改我改了出错了好几次都找不到原因可以轻松的将对象转换成对象和文档同样也可以将转换成对象和配置
1.创建一个web工程
2.在pom里面添加依赖,依赖不要随便改,我改了出错了好几次都找不到原因
</>复制代码
UTF-8
1.7
1.7
2.5.0
1.2
3.0-alpha-1
org.springframework
spring-webmvc
4.3.13.RELEASE
org.springframework
spring-web
4.3.13.RELEASE
org.springframework
spring-context
4.3.13.RELEASE
org.springframework
spring-beans
4.3.13.RELEASE
org.springframework
spring-core
4.3.13.RELEASE
org.springframework.security
spring-security-config
4.2.3.RELEASE
org.springframework.security
spring-security-web
4.2.3.RELEASE
com.fasterxml.jackson.core
jackson-core
${jacksonVersion}
com.fasterxml.jackson.core
jackson-annotations
${jacksonVersion}
com.fasterxml.jackson.core
jackson-databind
${jacksonVersion}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
org.codehaus.jackson
jackson-core-asl
1.9.13
jstl
jstl
${jstlVersion}
javax.servlet
servlet-api
${servletVersion}
provided
javax.servlet.jsp
jsp-api
2.2
provided
3.配置web.xml
</>复制代码
user-manager
index.jsp
contextConfigLocation
classpath:applicationContext.xml
classpath:springSecurity.xml
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
spring
/
4.配置applicationContext.xml
</>复制代码
5.配置springmvc.xml
</>复制代码
text/plain;charset=utf-8
text/html;charset=UTF-8
6.配置springSecurity.xml
</>复制代码
7.自定义成功处理的拦截器 MyAuthenticationSuccessHandler
</>复制代码
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private final static ObjectMapper objectMapper=new ObjectMapper();
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//这里是可以拿到用户对的登录名的
String username = request.getParameter("username");
System.out.println(username);
response.setContentType("application/json;charset=utf-8");
String successMessage = objectMapper.writeValueAsString(new JsonData(200, "登陆成功"));
response.getWriter().print(successMessage);
}
}
自定义失败处理器的拦截器
</>复制代码
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler{
private final static ObjectMapper objectMapper=new ObjectMapper();
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
//这里是可以拿到用户对的登录名的
String username = request.getParameter("username");
System.out.println(username);
response.setContentType("application/json;charset=utf-8");
String failureMessage = objectMapper.writeValueAsString(new JsonData(500, "登陆失败"));
response.getWriter().print(failureMessage);
}
}
自定义的authentication-provider下面的user-service
定义一个类,这个实现类就是用于封装数据库里面的用户的信息然后返回给springsecurity,它会比较从
表单穿过来的用户名和密码和数据库查出来的用户名和密码进行比对,这里写死,以后再加数据库
</>复制代码
public class MyUserDetailService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//UserDetails:封装用户数据的接口,这里应该是数据库查询出来的数据,当返回这个user对象时,springsecurity会把输入的用户名和密码和这个对象里面的用户名和密码进行比对
//成功则认证通过,失败则登陆失败
User user=new User("jojo","123456", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
return user;
}
}
创建这个目录结构
示范
</>复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
商品添加页面
商品添加页面
其它页面一样
index.jsp
</>复制代码
login.jsp
</>复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录页面
用户名:
密 码:
errorPage.jsp
</>复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
自定义错误页面
权限不足,请正确操作
controller
</>复制代码
@Controller
public class MainController {
/**
*
* @return登录页面
*/
@RequestMapping("/userLogin")
public String loginPage()
{
return "login";
}
/**
*
* @return登录页面
*/
@RequestMapping("/error")
public String errorPage()
{
return "errorPage";
}
}
</>复制代码
@Controller
@RequestMapping("product")
public class ProductController {
/**
* 商品添加
*/
@RequestMapping("index")
public String index()
{
return "index";
}
/**
* 商品添加
*/
@RequestMapping("add")
public String add()
{
return "product/productAdd";
}
/**
* 商品修改
*/
@RequestMapping("update")
public String update()
{
return "product/productUpdate";
}
/**
* 商品列表
*/
@RequestMapping("list")
public String list()
{
return "product/productList";
}
/**
* 商品删除
*/
@RequestMapping("delete")
public String delete()
{
return "product/productDelete";
}
}
创建一个返回给前台的包装类,用于返回code和message
</>复制代码
public class JsonData {
private Integer code;
private String message;
public JsonData() {
}
public JsonData(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/75711.html
摘要:什么是是一个能够为基于的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它来自于,那么它与整合开发有着天然的优势,目前与对应的开源框架还有。通常大家在做一个后台管理的系统的时候,应该采用判断用户是否登录。 什么是SpringSecurity ? Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全...
摘要:里面配置的过滤器链当用户使用表单请求时进入返回一个的实例一般是从数据库中查询出来的实例然后直接到最后一个如果有错则抛错给前面一个进行抛错如果没有错则放行可以访问对应的资源上面是总的执行流程下面单独说一下的认证流程这个图应该都看得懂和里面的配 showImg(https://segmentfault.com/img/bVbvO0O?w=1258&h=261);web.xml里面配置的过滤...
摘要:建立一个模块继承上一个模块然后添加依赖解决打包时找不到文件建立数据源文件数据库连接相关修改配置数据源和整合,以及事务管理自动扫描扫描时跳过注解的类控制器扫描配置文件这里指向的是 1.建立一个模块继承上一个模块然后添加依赖 junit junit 4.11 test ...
摘要:在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现的验证,这次和一起来配合使用加入的配置文件别名在的中配置数据源查找配置事物然后建立层,和层以及对应这里省略实 在上一篇基本配置了一些文件中,基本可以在文件中指定用户名和密码来进行实现SpringSecurity的验证,这次和mynatis一起来配合使用 加入mybatis的配置文件: mybatis-config....
摘要:的官方文档及其简单,他的示例配置就是在文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先导入包配置后面直接写这里会提示出错,提示找不 SpringSecurity的官方文档及其简单,他的示例配置就是在xml文件中把用户名和密码写固定了,然而在实际工作中是不可能的,参考了下网上的教程发现良莠不齐,特此写下记录学习过程首先po...
阅读 3294·2021-11-22 12:01
阅读 3905·2021-08-30 09:46
阅读 901·2019-08-30 13:48
阅读 3295·2019-08-29 16:43
阅读 1758·2019-08-29 16:33
阅读 1927·2019-08-29 13:44
阅读 1493·2019-08-26 13:45
阅读 2300·2019-08-26 11:44