├── src ├── main │ ├── resources │ │ └── application.properties │ └── java │ │ └── com │ │ └── example │ │ └── jwtauth │ │ ├── MyUserRepository.java │ │ ├── MyUser.java │ │ ├── JwtauthApplication.java │ │ ├── UserController.java │ │ ├── UserDetailsServiceImpl.java │ │ ├── MyWebSecurityConfig.java │ │ ├── JwtAuthenticationFilter.java │ │ └── JWTLoginFilter.java └── test │ └── java │ └── com │ └── example │ └── jwtauth │ └── JwtauthApplicationTests.java ├── .gitignore ├── README.md ├── gradlew.bat └── gradlew /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/MyUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | public interface MyUserRepository extends JpaRepository { 6 | MyUser findByUsername(String username); 7 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | /bin/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | nbproject/private/ 22 | build/ 23 | nbbuild/ 24 | dist/ 25 | nbdist/ 26 | .nb-gradle/ 27 | -------------------------------------------------------------------------------- /src/test/java/com/example/jwtauth/JwtauthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class JwtauthApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jwt-boot-auth 2 | secure spring-boot APIs with JWT 3 | 4 | #### 请求hello接口,由于没有登录,会收到403错误 5 | curl http://localhost:8080/hello 6 | 7 | #### 注册一个新用户 8 | curl -H "Content-Type: application/json" -X POST -d '{ 9 | "username": "admin", 10 | "password": "password" 11 | }' http://localhost:8080/users/signup 12 | 13 | #### 登录,会返回token,在http header中,Authorization: Bearer 后面的部分就是token 14 | curl -i -H "Content-Type: application/json" -X POST -d '{ 15 | "username": "admin", 16 | "password": "password" 17 | }' http://localhost:8080/login 18 | 19 | #### 用登录成功后拿到的token再次请求hello接口 20 | 21 | 将请求中的XXXXXX替换成拿到的token,这次可以成功调用接口了 22 | 23 | curl -H "Content-Type: application/json" \ 24 | -H "Authorization: Bearer XXXXXX" \ 25 | "http://localhost:8080/hello" 26 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/MyUser.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | @Entity 9 | public class MyUser { 10 | @Id 11 | @GeneratedValue(strategy = GenerationType.IDENTITY) 12 | private long id; 13 | private String username; 14 | private String password; 15 | 16 | public long getId() { 17 | return id; 18 | } 19 | 20 | public String getUsername() { 21 | return username; 22 | } 23 | 24 | public void setUsername(String username) { 25 | this.username = username; 26 | } 27 | 28 | public String getPassword() { 29 | return password; 30 | } 31 | 32 | public void setPassword(String password) { 33 | this.password = password; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/JwtauthApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @SpringBootApplication 12 | @RestController 13 | public class JwtauthApplication { 14 | 15 | @Bean 16 | public BCryptPasswordEncoder bCryptPasswordEncoder() { 17 | return new BCryptPasswordEncoder(); 18 | } 19 | 20 | @RequestMapping("/hello") 21 | @ResponseBody 22 | public String hello(){ 23 | return "hello"; 24 | } 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(JwtauthApplication.class, args); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 4 | import org.springframework.web.bind.annotation.PostMapping; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @RequestMapping("/users") 11 | public class UserController { 12 | 13 | private MyUserRepository applicationUserRepository; 14 | private BCryptPasswordEncoder bCryptPasswordEncoder; 15 | 16 | public UserController(MyUserRepository myUserRepository, 17 | BCryptPasswordEncoder bCryptPasswordEncoder) { 18 | this.applicationUserRepository = myUserRepository; 19 | this.bCryptPasswordEncoder = bCryptPasswordEncoder; 20 | } 21 | 22 | @PostMapping("/signup") 23 | public void signUp(@RequestBody MyUser user) { 24 | user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); 25 | applicationUserRepository.save(user); 26 | } 27 | } -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/UserDetailsServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.springframework.security.core.userdetails.User; 4 | import org.springframework.security.core.userdetails.UserDetails; 5 | import org.springframework.security.core.userdetails.UserDetailsService; 6 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 | import org.springframework.stereotype.Service; 8 | 9 | import static java.util.Collections.emptyList; 10 | 11 | @Service 12 | public class UserDetailsServiceImpl implements UserDetailsService { 13 | private MyUserRepository applicationUserRepository; 14 | 15 | public UserDetailsServiceImpl(MyUserRepository applicationUserRepository) { 16 | this.applicationUserRepository = applicationUserRepository; 17 | } 18 | 19 | @Override 20 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 | MyUser applicationUser = applicationUserRepository.findByUsername(username); 22 | if (applicationUser == null) { 23 | throw new UsernameNotFoundException(username); 24 | } 25 | return new User(applicationUser.getUsername(), applicationUser.getPassword(), emptyList()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/MyWebSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import org.springframework.boot.autoconfigure.security.SecurityProperties; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.core.annotation.Order; 6 | import org.springframework.http.HttpMethod; 7 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 11 | import org.springframework.security.core.userdetails.UserDetailsService; 12 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 13 | 14 | @Configuration 15 | @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 16 | public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter { 17 | private UserDetailsService userDetailsService; 18 | private BCryptPasswordEncoder bCryptPasswordEncoder; 19 | 20 | public MyWebSecurityConfig(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) { 21 | this.userDetailsService = userDetailsService; 22 | this.bCryptPasswordEncoder = bCryptPasswordEncoder; 23 | } 24 | 25 | @Override 26 | protected void configure(HttpSecurity http) throws Exception { 27 | http.cors().and().csrf().disable().authorizeRequests() 28 | .antMatchers(HttpMethod.POST, "/users/signup").permitAll() 29 | .anyRequest().authenticated() 30 | .and() 31 | .addFilter(new JWTLoginFilter(authenticationManager())) 32 | .addFilter(new JwtAuthenticationFilter(authenticationManager())); 33 | } 34 | 35 | @Override 36 | public void configure(AuthenticationManagerBuilder auth) throws Exception { 37 | auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/JwtAuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import io.jsonwebtoken.Jwts; 4 | import org.springframework.security.authentication.AuthenticationManager; 5 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 6 | import org.springframework.security.core.context.SecurityContextHolder; 7 | import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; 8 | 9 | import javax.servlet.FilterChain; 10 | import javax.servlet.ServletException; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | import java.io.IOException; 14 | import java.util.ArrayList; 15 | 16 | public class JwtAuthenticationFilter extends BasicAuthenticationFilter { 17 | public JwtAuthenticationFilter(AuthenticationManager authManager) { 18 | super(authManager); 19 | } 20 | 21 | @Override 22 | protected void doFilterInternal(HttpServletRequest req, 23 | HttpServletResponse res, 24 | FilterChain chain) throws IOException, ServletException { 25 | String header = req.getHeader("Authorization"); 26 | 27 | if (header == null || !header.startsWith("Bearer ")) { 28 | chain.doFilter(req, res); 29 | return; 30 | } 31 | 32 | UsernamePasswordAuthenticationToken authentication = getAuthentication(req); 33 | 34 | SecurityContextHolder.getContext().setAuthentication(authentication); 35 | chain.doFilter(req, res); 36 | } 37 | 38 | private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) { 39 | String token = request.getHeader("Authorization"); 40 | if (token != null) { 41 | // parse the token. 42 | String user = Jwts.parser() 43 | .setSigningKey("MyJwtSecret") 44 | .parseClaimsJws(token.replace("Bearer ", "")) 45 | .getBody() 46 | .getSubject(); 47 | 48 | if (user != null) { 49 | return new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>()); 50 | } 51 | return null; 52 | } 53 | return null; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/java/com/example/jwtauth/JWTLoginFilter.java: -------------------------------------------------------------------------------- 1 | package com.example.jwtauth; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import io.jsonwebtoken.Jwts; 5 | import io.jsonwebtoken.SignatureAlgorithm; 6 | import org.springframework.security.authentication.AuthenticationManager; 7 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 8 | import org.springframework.security.core.Authentication; 9 | import org.springframework.security.core.AuthenticationException; 10 | import org.springframework.security.core.userdetails.User; 11 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 12 | 13 | import javax.servlet.FilterChain; 14 | import javax.servlet.ServletException; 15 | import javax.servlet.http.HttpServletRequest; 16 | import javax.servlet.http.HttpServletResponse; 17 | import java.io.IOException; 18 | import java.util.ArrayList; 19 | import java.util.Date; 20 | 21 | public class JWTLoginFilter extends UsernamePasswordAuthenticationFilter { 22 | private AuthenticationManager authenticationManager; 23 | 24 | public JWTLoginFilter(AuthenticationManager authenticationManager) { 25 | this.authenticationManager = authenticationManager; 26 | } 27 | 28 | @Override 29 | public Authentication attemptAuthentication(HttpServletRequest req, 30 | HttpServletResponse res) throws AuthenticationException { 31 | try { 32 | MyUser user = new ObjectMapper() 33 | .readValue(req.getInputStream(), MyUser.class); 34 | 35 | return authenticationManager.authenticate( 36 | new UsernamePasswordAuthenticationToken( 37 | user.getUsername(), 38 | user.getPassword(), 39 | new ArrayList<>()) 40 | ); 41 | } catch (IOException e) { 42 | throw new RuntimeException(e); 43 | } 44 | } 45 | 46 | @Override 47 | protected void successfulAuthentication(HttpServletRequest req, 48 | HttpServletResponse res, 49 | FilterChain chain, 50 | Authentication auth) throws IOException, ServletException { 51 | 52 | String token = Jwts.builder() 53 | .setSubject(((User) auth.getPrincipal()).getUsername()) 54 | .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000)) 55 | .signWith(SignatureAlgorithm.HS512, "MyJwtSecret") 56 | .compact(); 57 | res.addHeader("Authorization", "Bearer " + token); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save ( ) { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | --------------------------------------------------------------------------------