
1、如果有配置类似nexus这样的服务器的话,可以在3rd party中进行添加,具体如何 *** 作,可以度娘。
2、通过mvn命令行的方式,将jar包安装到本地maven库中。
3、可以在项目中建一个文件夹,将jar包copy到文件夹中,然后在pom.xml 的
dependencies这样添加:
<dependency>
<groupId>yourjarName</groupId>
<artifactId>yourjarName-api</artifactId>
<scope>system</scope>
<version>anyversion</version>
<systemPath>${project.basedir}/lib/yourjar.jar</systemPath>
</dependency>
MySQLSQLite
Android Studio如何在项目中添加gson-2.2.3.jar?
标签: Android Android-Studio
我尝试使用新的Android Studio,但我似乎无法让它正常工作.
我使用Gson类库来序列化/反序列化JSON对象.但是类库不知何故无法包含在项目里.
我创建一个新的项目只用MainActivity.在/libs文件夹里复制gson-2.2.3.jar并添加它为一个类库.这样在android studio里包含了jar,因此它可以从源文件被引用.
当我试着运行项目,但是它无法被编译,所以我在.gradle文件里添加:
1
compile files('libs/gson-2.2.3.jar')
到依赖项里.然后我成功编译了项目,但是当我运行应用程序后,得到ClassDefNotFoundException异常.
解决方法 1:
把Gson jar(在我的项目里,是gson-2.2.4.jar)放入libs文件夹
右击它,然后点击 'Add as library'
确保编译文件('libs/gson-2.2.4.jar')在你的build.gradle文件里
注意:
第二步是关键步骤,如果漏了,将出现你无法运行的问题.
背景分析传统的登录系统中,每个站点都实现了自己的专用登录模块。各站点的登录状态相互不认可,各站点需要逐一手工登录。例如:
这样的系统,我们又称之为多点登陆系统。应用起来相对繁琐(每次访问资源服务都需要重新登陆认证和授权)。与此同时,系统代码的重复也比较高。由此单点登陆系统诞生。
2、单点登陆系统
单点登录,英文是 Single Sign On(缩写为 SSO)。即多个站点共用一台认证授权服务器,用户在其中任何一个站点登录后,可以免登录访问其他所有站点。而且,各站点间可以通过该登录状态直接交互。例如:
二、快速入门实践
1、工程结构如下
基于资源服务工程添加单点登陆认证和授权服务,工程结构定义如下:
2、创建认证授权工程
3、添加项目依赖
<dependencies>
<!--Spring Boot Web (服务-内置tomcat)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Nacos Discovery (服务注册发现)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Nacos Config (配置中心)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--认证、授权-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
</dependencies>
登录后复制
4、构建项目配置文件
在sca-auth工程中创建bootstrap.yml文件,例如:
server:
port: 8071
spring:
application:
name: sca-auth
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
登录后复制
5、添加项目启动类
package com.jt
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
public class ResourceAuthApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceAuthApplication.class,args)
}
}
登录后复制
6、启动并访问项目
项目启动时,系统会默认生成一个登陆密码,例如:
开浏览器输入http://localhost:8071呈现登陆页面,默认用户:user 例如:
其中,默认用户名为user,密码为系统启动时,在控制台呈现的密码。执行登陆测试,登陆成功进入如下界面(因为没有定义登陆页面,所以会出现404):
三、自定义登陆逻辑
1、业务描述
我们的单点登录系统最终会按照如下结构进行设计和实现,例如:
我们在实现登录时,会在UI工程中,定义登录页面(login.html),然后在页面中输入自己的登陆账号,登陆密码,将请求提交给网关,然后网关将请求转发到auth工程,登陆成功和失败要返回json数据,在这个章节我们会按这个业务逐步进行实现
2、定义安全配置类
修改SecurityConfig配置类,添加登录成功或失败的处理逻辑,例如:
package com.jt.auth.config
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.web.authentication.AuthenticationFailureHandler
import org.springframework.security.web.authentication.AuthenticationSuccessHandler
import javax.servlet.http.HttpServletResponse
import java.io.IOException
import java.io.PrintWriter
import java.util.HashMap
import java.util.Map
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**初始化密码加密对象*/
@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder()
}
/**配置认证管理器(此对象主要负责对客户端输入的用户信息进行认证),
* 在其它配置类中会用到这个对象*/
@Bean
public AuthenticationManager authenticationManagerBean()
throws Exception {
return super.authenticationManagerBean()
}
/**在这个方法中定义登录规则
* 1)对所有请求放行(当前工程只做认证)
* 2)登录成功信息的返回
* 3)登录失败信息的返回
* */
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭跨域工具
http.csrf().disable()
//放行所有请求
http.authorizeRequests().anyRequest().permitAll()
//登录成功与失败的处理
http.formLogin()
.successHandler(successHandler())
.failureHandler(failureHandler())
}
@Bean
public AuthenticationSuccessHandler successHandler(){
//return new AuthenticationSuccessHandler() {
//@Override
//public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//
//}
//}
return (request,response,authentication) ->{
//1.构建map对象,封装响应数据
Map<String,Object>map=new HashMap<>()
map.put("state",200)
map.put("message","login ok")
//2.将map对象写到客户端
writeJsonToClient(response,map)
}
}
@Bean
public AuthenticationFailureHandler failureHandler(){
return (request,response, e)->{
//1.构建map对象,封装响应数据
Map<String,Object>map=new HashMap<>()
map.put("state",500)
map.put("message","login failure")
//2.将map对象写到客户端
writeJsonToClient(response,map)
}
}
private void writeJsonToClient(HttpServletResponse response,
Object object) throws IOException {
//1.将对象转换为json
//将对象转换为json有3种方案:
//1)Google的Gson-->toJson (需要自己找依赖)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)