我需要部署我的web应用,后端:spring前端:angular
angular与电容器集成,因为我需要在android上运行fe
我在远程主机上部署了spring应用程序,并安装了apache2+tomcat。
当我将spring引导war文件部署到tomcat中时,除了authenticate之外,所有的http请求都可以工作,它使用jwt对用户进行身份验证。当我尝试执行该请求时,与服务器的连接停止。是否与jwts有任何可能导致方法失败的联系?
我真的不明白为什么因为离线工作没有任何问题。你们能帮帮我吗?
这是身份验证控制器
@RestController
@CrossOrigin
public class JwtAuthenticationController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private JwtUserDetailsService userDetailsService;
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception {
authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
final UserDetails userDetails = userDetailsService
.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token, userDetails));
}
private void authenticate(String username, String password) throws Exception {
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (DisabledException e) {
throw new Exception("USER_DISABLED", e);
} catch (BadCredentialsException e) {
throw new Exception("INVALID_CREDENTIALS", e);
}
}
}
这是pom.xml
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>leopardiproject.csd.CsdApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>com.braintreepayments.gateway</groupId>
<artifactId>braintree-java</artifactId>
<version>2.79.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>giacomoLeopardi</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
这是从我的客户端执行http post请求的服务:
export class User{
constructor(
public username: string,
public password: string,
public name: string,
public lastname: string,
public checkbox1: boolean,
public checkbox2: boolean,
public checkbox3: boolean,
public enabled: boolean,
public roles: Role
) {}
}
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
constructor(private httpClient:HttpClient, private router: Router, private location: Location) {
}
authenticate(username, password) {
//EDITED
return this.httpClient.post<any>('http://remote-ip-address:8080/authenticate',{username,password}).pipe(
map(
userData => {
sessionStorage.setItem('username',username);
let tokenStr= userData.token;
sessionStorage.setItem('token', tokenStr);
sessionStorage.setItem('Role', userData.utente.authorities[0].authority);
return userData;
}
)
);
}
logOut() {
sessionStorage.removeItem('username');
sessionStorage.removeItem('token');
sessionStorage.removeItem('Role');
this.router.navigate(['/home']).then(() => {
window.location.reload();
});
}
getRole(){
let token = jwt_decode(sessionStorage.getItem('token'));
return token['role'];
}
}
1条答案
按热度按时间sg24os4d1#
嘿,金属识别器,
我注意到您在authenticationservice中编写的帖子url为:
因此,这将始终指向您的localhost(即使在生产环境中),您应该查找您的api的ip或fqdn,以便您可以指向正确的位置。
理想情况下,这类信息应该存储在环境变量中,因此我会将其存储在environment.ts文件中,例如:
然后您可以在服务中使用它: