当前位置: 首页 > news >正文

SpringBoot(Security)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分
用户认证
  指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。
  一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。

用户授权
  指的是验证某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的
  一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限

  用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 
  表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。
  
  在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control
  List,ACL),可以对应用中的领域对象进行细粒度的控制。

 

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

xx.properties

spring.datasource.url=jdbc:mysql://localhost:3306/xx_dev?useSSL=false
spring.datasource.username=xx
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.user_ds.url=jdbc:mysql://localhost:3306/xx_user_dev?useSSL=false
spring.user_ds.username=xx
spring.user_ds.password=123
spring.user_ds.driver-class-name=com.mysql.jdbc.Driver

XXXSceurity.java

@Configuration
@EnableWebSecurity
public class SecurityConf extends WebSecurityConfigurerAdapter{

  @Autowired
  @Qualifier("userDetailsService")
  private UserDetailsService userDetailsService;

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
    //auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    super.configure(auth);
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
         .antMatchers("/zcs/user/register").permitAll()
         .antMatchers("/zcs/user/enable").permitAll()
         .antMatchers("/zcs/user/disable").permitAll()
//       .antMatchers("/zcs/user/register").hasAuthority("ADMIN")
//       .antMatchers("/zcs/user/enable").hasAuthority("ADMIN")
//       .antMatchers("/zcs/user/disable").hasAuthority("ADMIN")
         .antMatchers("/zcs/user/login").hasAuthority("USER")
         .antMatchers("/zcs/user/logout").hasAuthority("USER")
         .antMatchers("/zcs/admin/login").hasAuthority("ADMIN")
         .antMatchers("/zcs/admin/logout").hasAuthority("ADMIN")
         .antMatchers("/zcs/**", "/ys/**").hasAnyAuthority("USER","ADMIN")
         .antMatchers("/**").permitAll()
         .anyRequest().fullyAuthenticated()
         .and()
         .exceptionHandling()
         .authenticationEntryPoint(new AjaxLoginUrlAuthenticationEntryPoint())
         .and()
 		 .formLogin()
 		 .loginProcessingUrl("/zcs/user/login")
// 		 .successForwardUrl("/user/loginSuccess")
// 		 .failureForwardUrl("/user/loginFailure")
 		 .successHandler(new AjaxLoginUrlAuthenticationSuccessHandler())
 		 .failureHandler(new AjaxLoginUrlAuthenticationFailureHandler())
         .usernameParameter("username")
         .passwordParameter("password")
         .permitAll()
         .and()
         .logout()
         .logoutUrl("/zcs/user/logout")
         .addLogoutHandler(new AjaxLogoutHandler())
//         .logoutSuccessUrl("/user/loginSueecess")
//         .logoutSuccessHandler(new AjaxLogoutSuccessHandler())
         .deleteCookies("remember-me")
         .permitAll()
         .and()
         .rememberMe()
         .and()
         .csrf()
         .disable()
//         .and()
//		 .csrf().requireCsrfProtectionMatcher(
//			new AndRequestMatcher(
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs/user/**", HttpMethod.POST.toString())),
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs/user/**", HttpMethod.OPTIONS.toString())),
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs*/**", HttpMethod.GET.toString())),
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs*/**", HttpMethod.HEAD.toString())),
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs*/**", HttpMethod.OPTIONS.toString())),
//				new NegatedRequestMatcher(new AntPathRequestMatcher("/zcs*/**", HttpMethod.TRACE.toString()))
//			)
//		)
//		.and()
//        .addFilterBefore(new CorsHeaderFilter(), ChannelProcessingFilter.class)
//		.addFilterAfter(new CsrfCookieFilter(), CsrfFilter.class)
		;

}
public class AjaxAuthenticationData {
  private int status;
  private String message;

  public AjaxAuthenticationData(int status, String message) {
    super();
    this.status = status;
    this.message = message;
  }

  public int getStatus() {
    return status;
  }
  public void setStatus(int status) {
    this.status = status;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
}

XXXXHandlers 

public class AjaxLoginUrlAuthenticationEntryPoint implements AuthenticationEntryPoint {
  private static final Log logger = LogFactory.getLog(AjaxLoginUrlAuthenticationEntryPoint.class);

  /**
   * Performs the redirect (or forward) to the login form URL.
   */
  @Override
  public void commence(HttpServletRequest request, HttpServletResponse response,
      AuthenticationException authException) throws IOException, ServletException {

    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + authException.getMessage());
  }

}


public class AjaxLoginUrlAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
      Authentication authentication) throws IOException, ServletException {

    ObjectMapper objectMapper = new ObjectMapper();
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    try {
      AuthenticationData json = new AuthenticationData(0, "Ajax auth succeeds!");
      objectMapper.writeValue(response.getOutputStream(), json);
    } catch (JsonProcessingException ex) {
      throw new HttpMessageNotWritableException("Error to write JSON: " + ex.getMessage(), ex);
    }

    super.onAuthenticationSuccess(request, response, authentication);
  }
}


public class AjaxLoginUrlAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
  @Override
  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
      AuthenticationException exception) throws IOException, ServletException {

    ObjectMapper objectMapper = new ObjectMapper();
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    try {
      AuthenticationData json = new AuthenticationData(1, "Ajax auth failure!");
      objectMapper.writeValue(response.getOutputStream(), json);
    } catch (JsonProcessingException ex) {
      throw new HttpMessageNotWritableException("Error to write JSON: " + ex.getMessage(), ex);
    }

  //		super.onAuthenticationFailure(request, response, exception);
  }

}


public class AjaxLogoutHandler extends SecurityContextLogoutHandler {
	@Override
	public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication){
		ObjectMapper objectMapper = new ObjectMapper();
		response.setHeader("Content-Type", "application/json;charset=UTF-8");
		try {
			AuthenticationData json = new AuthenticationData(0, "Ajax auth Logout!");
			objectMapper.writeValue(response.getOutputStream(), json);
		} catch (IOException ex) {
			throw new HttpMessageNotWritableException("Error to write JSON: " + ex.getMessage(), ex);
		}

		super.logout(request, response, authentication);
	}
}


public class AjaxLoginUrlAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {

		ObjectMapper objectMapper = new ObjectMapper();
		response.setHeader("Content-Type", "application/json;charset=UTF-8");
		try {
			AuthenticationData json = new AuthenticationData(0, "Ajax auth succeeds!");
			objectMapper.writeValue(response.getOutputStream(), json);
		} catch (JsonProcessingException ex) {
			throw new HttpMessageNotWritableException("Error to write JSON: " + ex.getMessage(), ex);
		}

		super.onAuthenticationSuccess(request, response, authentication);
	}
}


public class CorsHeaderFilter implements Filter {
  public static final String REQUEST_HEADER_NAME = "X-CSRF-TOKEN";

  private final List<String> allowedOrigins = Arrays.asList("http://localhost:8080",
      "http://127.0.0.1:8080",
      "http://192.168.1.53:8080",
          "http://cuikexi:8080");

  public void destroy() {
    //do nothing
  }

  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {
    if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;

      // Access-Control-Allow-Origin
      String origin = request.getHeader("Origin");
      response.setHeader("Access-Control-Allow-Origin", allowedOrigins.contains(origin) ? origin : "");
      response.setHeader("Vary", "Origin");
      response.setHeader("Access-Control-Max-Age", "3600");
      response.setHeader("Access-Control-Allow-Credentials", "true");
      response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
      response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, " + REQUEST_HEADER_NAME);
    }

    chain.doFilter(req, res);
  }

  public void init(FilterConfig filterConfig) {
    //do nothing
  }
}

public class CsrfCookieFilter extends OncePerRequestFilter {

  public static final String RESPONSE_COOKIE_NAME = "CSRF-TOKEN";

  protected static final String REQUEST_ATTRIBUTE_NAME = "_csrf";

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

    CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME);

    Cookie cookie = new Cookie(RESPONSE_COOKIE_NAME, token.getToken());
    cookie.setPath("/");

    response.addCookie(cookie);

    filterChain.doFilter(request, response);
  }
}

client(js)

var request_data = {'username':'zm','password':'123456'};
$.ajax({
    type: "POST",
    url: "/zcs/user/login",
    data: request_data,
    success: function(msg){
        alert( "Response Data: " + msg );
    }
});

client(curl)

#直接无权访问
#curl -XGET -uzhaomeng:123456 http://127.0.0.1:8080/zcs/input/suggest/list?term=teng
#curl -XGET -uzhaomeng2:123456 http://127.0.0.1:8080/zcs/input/suggest/list?term=teng
#登陆验证
curl -XPOST -d'username=zhaomeng&password=123456'  http://127.0.0.1:8080/zcs/user/login
curl -XPOST -d'username=zhaomeng2&password=123456'  http://127.0.0.1:8080/zcs/user/login
#直接授权访问
#curl -XGET -uzhaomeng:123456 http://127.0.0.1:8080/zcs/input/suggest/list?term=teng
#curl -XGET -uzhaomeng2:123456 http://127.0.0.1:8080/zcs/input/suggest/list?term=teng
#登出验证
curl -XPOST -d'username=zhaomeng&password=123456'  http://127.0.0.1:8080/zcs/user/login
curl -XPOST -d'username=zhaomeng2&password=123456'  http://127.0.0.1:8080/zcs/user/login

 

转载于:https://my.oschina.net/igooglezm/blog/909176

相关文章:

  • 初学Sockets编程(二) 关于名称和地址族
  • HDU - 1166 敌兵布阵
  • Flask+腾讯云windows主机快速搭建微信公众号接口
  • 一、简单工厂模式
  • 微软将所有的Windows代码库迁移到Git
  • magento megatron主题加入中文
  • 对象不支持“abigimage”属性或方法
  • Hyper-v创建检查点(VM的快照功能)
  • dede程序打开install安装时出现dir
  • 解答《编程之美》1.18问题1:给所有未标识方块标注有地雷概率
  • 【EMC】基本概念
  • Netty断线重连
  • 不要小看了get 与set
  • redhat7.2升级openssl、openssh
  • [HAOI2016]食物链
  • SegmentFault for Android 3.0 发布
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • Angular 响应式表单 基础例子
  • axios 和 cookie 的那些事
  • Docker容器管理
  • input实现文字超出省略号功能
  • java8 Stream Pipelines 浅析
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • Netty源码解析1-Buffer
  • node.js
  • Objective-C 中关联引用的概念
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • SpiderData 2019年2月13日 DApp数据排行榜
  • 大数据与云计算学习:数据分析(二)
  • 聚类分析——Kmeans
  • 前端攻城师
  • 使用API自动生成工具优化前端工作流
  • 微信公众号开发小记——5.python微信红包
  • 异步
  • 用jquery写贪吃蛇
  • Semaphore
  • zabbix3.2监控linux磁盘IO
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (31)对象的克隆
  • (AngularJS)Angular 控制器之间通信初探
  • (C++17) optional的使用
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)大型网站架构演变和知识体系
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .Net 中Partitioner static与dynamic的性能对比
  • .NET轻量级ORM组件Dapper葵花宝典
  • @Autowired自动装配
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • [ 转载 ] SharePoint 资料