【发布时间】:2015-05-15 17:00:02
【问题描述】:
我正在尝试使用基于令牌的身份验证 jhipster。效果很好。
现在,我想在不同的域上运行后端和前端代码。我该怎么做?
这是我尝试过的:
-
运行
yo jhipster并选择基于令牌的身份验证选项:Welcome to the JHipster Generator ? (1/13) What is the base name of your application? jhipster ? (2/13) What is your default Java package name? com.mycompany.myapp ? (3/13) Do you want to use Java 8? Yes (use Java 8) ? (4/13) Which *type* of authentication would you like to use? Token-based authentication (stateless, with a token) ? (5/13) Which *type* of database would you like to use? SQL (H2, MySQL, PostgreSQL) ? (6/13) Which *production* database would you like to use? MySQL ? (7/13) Which *development* database would you like to use? H2 in-memory with Web console ? (8/13) Do you want to use Hibernate 2nd level cache? Yes, with ehcache (local cache, for a single node) ? (9/13) Do you want to use clustered HTTP sessions? No ? (10/13) Do you want to use WebSockets? No ? (11/13) Would you like to use Maven or Gradle for building the backend? Maven (recommended) ? (12/13) Would you like to use Grunt or Gulp.js for building the frontend? Grunt (recommended) ? (13/13) Would you like to use the Compass CSS Authoring Framework? No ... I'm all done. Running bower install & npm install for you ^C 将项目复制为
jhipster/backend和jhipster/frontend-
从后端和前端删除不必要的文件
rm -rf backend/.bowerrc rm -rf backend/.jshintrc rm -rf backend/bower.json rm -rf backend/Gruntfile.js rm -rf backend/package.json rm -rf backend/src/main/webapp rm -rf backend/src/test/javascript rm -rf frontend/pom.xml rm -rf frontend/src/main/java rm -rf frontend/src/main/resources rm -rf frontend/src/test/gatling rm -rf frontend/src/test/java rm -rf frontend/src/test/resources -
更改代码以完全消除后端/前端依赖性
-
frontend/Gruntfile.js... var parseVersionFromPomXml = function() { return '1.2.2.RELEASE'; }; ... browserSync: { ..., proxy: "localhost:8081" } -
frontend/src/main/webapp/scripts/app/app.jsangular.module('jhipsterApp', [...]) .constant('API_URL', 'http://localhost:8080/') .run( ... ) -
frontend/src/main/webapp/scripts/**/*.service.jsangular.module('jhipsterApp').factory(..., API_URL) { return $http.post(API_URL + 'api/authenticate', ...); } angular.module('jhipsterApp').factory('Account', function Account($resource, API_URL) { return $resource(API_URL + 'api/account', {}, {...}); } // Make similar changes in all service files. -
backend/pom.xml删除 yeoman-maven-plugin
-
backend/src/main/java/com/mycompany/myapp/SimpleCORSFilter.java// Copied from here: https://spring.io/guides/gs/rest-service-cors/ @Component public class SimpleCORSFilter implements Filter { public void doFilter(...) { ... response.setHeader("Access-Control-Allow-Origin", "*"); ... } }
-
-
运行
-
终端标签 #1:BACKEND
cd backend mvn spring-boot:run ... [INFO] com.mycompany.myapp.Application - Started Application in 11.529 seconds (JVM running for 12.079) [INFO] com.mycompany.myapp.Application - Access URLs: ---------------------------------------------------------- Local: http://127.0.0.1:8080 External: http://192.168.56.1:8080 ---------------------------------------------------------- -
终端标签 #2:前端
cd frontend/src/main/webapp npm install -g http-server http-server Starting up http-server, serving ./ on: http://0.0.0.0:8081 Hit CTRL-C to stop the server -
终端标签 #3:咕噜声
cd frontend bower install npm install grunt serve ... [BS] Proxying: http://localhost:8081 [BS] Access URLs: ------------------------------------- Local: http://localhost:3000 External: http://10.34.16.128:3000 ------------------------------------- UI: http://localhost:3001 UI External: http://10.34.16.128:3001 -------------------------------------
-
-
浏览http://localhost:3000/#/login
输入
username:password为admin:admin我们的 BACKEND 标签显示:
[DEBUG] com.mycompany.myapp.security.Http401UnauthorizedEntryPoint - Pre-authenticated entry point called. Rejecting access
显然,我做错了什么。这是什么?
【问题讨论】:
-
只是一个想法,但也许您可以仔细检查后端是否能够处理有效凭据。见stackoverflow.com/questions/28269487/…
-
你有没有设法让它运行?
-
@Daniel 不幸的是,没有。在我提出这个问题后,我很快就停止玩 jhipster 了。
-
@musa 我下面的回答帮助了其他一些人,也是我解决问题的方法。请查看并考虑将其标记为已接受的答案。
标签: java angularjs authentication jwt jhipster