springboot错误——使用vue在登录页面登录时,提示错误信息500

在这里插入图片描述
在登录页面输入账号密码登录时出现错误,打开控制台提示500:
找了许久找到了错误:
在这里插入图片描述

在配置项中,server拼写错误,但没有给任何提示,造成了服务器500错误
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为了实现SpringBoot+Vue前后端分离的登录界面使用JWT鉴权,需要进行以下步骤: 1.在SpringBoot中配置SpringSecurity,包括用户认证和授权,可以参考引用中的实现方式。 2.在SpringBoot中配置JWT,包括生成Token和解析Token,可以使用jjwt库实现,可以参考引用中的maven依赖配置。 3.在Vue中实现登录界面,包括输入用户名和密码,发送POST请求到后端验证用户信息,并获取Token。 4.在Vue使用获取到的Token进行后续请求的鉴权,可以在请求头中添加Authorization字段,值为Bearer加上Token。 下面是一个简单的示例代码,仅供参考: 1. SpringBoot中的配置 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired private JwtAuthenticationEntryPoint unauthorizedHandler; @Bean public JwtAuthenticationFilter jwtAuthenticationFilter() { return new JwtAuthenticationFilter(); } @Override public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { authenticationManagerBuilder .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Bean(BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http .cors() .and() .csrf() .disable() .exceptionHandling() .authenticationEntryPoint(unauthorizedHandler) .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers("/api/auth/**") .permitAll() .anyRequest() .authenticated(); // Add our custom JWT security filter http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } } ``` 2. Vue中的登录界面 ```html <template> <div> <h2>Login</h2> <form @submit.prevent="login"> <div> <label>Username:</label> <input type="text" v-model="username" required> </div> <div> <label>Password:</label> <input type="password" v-model="password" required> </div> <button type="submit">Login</button> </form> </div> </template> <script> import axios from 'axios'; export default { data() { return { username: '', password: '' }; }, methods: { async login() { try { const response = await axios.post('/api/auth/login', { username: this.username, password: this.password }); const token = response.data.token; localStorage.setItem('token', token); axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; this.$router.push('/'); } catch (error) { console.error(error); } } } }; </script> ``` 3. Vue中的请求鉴权 ```javascript import axios from 'axios'; const token = localStorage.getItem('token'); if (token) { axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; } axios.get('/api/protected/resource') .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫医初春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值